• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Feb 25th, 2019 196 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1.     import graph;
2.
3.     // parameters to tweak
4.
5.     real start = 0;
6.     real end = 2;
7.
8.     real graphleft = -1;
9.     real graphright = 3;
10.
11.     real f(real x) {
12.         return x * x;
13.     }
14.
15.     real fderiv(real x) {
16.         return 2 * x;
17.     }
18.
19.     real jumpsize = 1;
20.
22.
23.     // stuff that draws
24.     real jumps = (end - start) / jumpsize;
25.
26.     path g = graph(f, graphleft, graphright);
27.     draw(g, blue);
28.
29.     real height = f(start);
30.     for (int n = 0; n < jumps; ++n) {
31.         pair p1 = (start + jumpsize * n, height);
32.
33.         real x2 = start + jumpsize * (n+1);
34.         real y2 = height + fderiv(x2) * jumpsize;
35.         pair p2 = (x2, y2);
36.
37.         draw(p1 -- p2, red);
38.         height = y2;
39.     }
40.
41.     // draw points afterwards so they aren't covered by segments
42.     height = f(start);
43.     for (int n = 0; n <= jumps; ++n) {
44.         real x = start + jumpsize * n;
45.
46.             pair p = (x, height);
47.             height = height + fderiv(x + jumpsize) * jumpsize;
48.             pair p_actual = (x, f(x));
49.
50.             draw(p -- p_actual, dashed);