Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import graph;
- // parameters to tweak
- real start = 0;
- real end = 2;
- real graphleft = -1;
- real graphright = 3;
- real f(real x) {
- return x * x;
- }
- real fderiv(real x) {
- return 2 * x;
- }
- real jumpsize = 1;
- real pointradius = 0.05;
- // stuff that draws
- real jumps = (end - start) / jumpsize;
- path g = graph(f, graphleft, graphright);
- draw(g, blue);
- real height = f(start);
- for (int n = 0; n < jumps; ++n) {
- pair p1 = (start + jumpsize * n, height);
- real x2 = start + jumpsize * (n+1);
- real y2 = height + fderiv(x2) * jumpsize;
- pair p2 = (x2, y2);
- draw(p1 -- p2, red);
- height = y2;
- }
- // draw points afterwards so they aren't covered by segments
- height = f(start);
- for (int n = 0; n <= jumps; ++n) {
- real x = start + jumpsize * n;
- pair p = (x, height);
- height = height + fderiv(x + jumpsize) * jumpsize;
- pair p_actual = (x, f(x));
- draw(p -- p_actual, dashed);
- fill(circle(p, pointradius), purple);
- fill(circle(p_actual, pointradius), green);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement