1 function df = forward_euler(integer_order, h, f, x)
3 ## Use the forward Euler method to compute the derivative of `f` at
8 ## * ``integer_order`` - The order of the derivative.
10 ## * ``h`` - The step size.
12 ## * ``f`` - The function whose derivative we're computing.
14 ## * ``x`` - The point at which to compute the derivative.
17 if (integer_order == 0)
22 ## We need a few points around `x` to compute the derivative at `x`.
23 ## The number of points depends on the order.
24 if (even(integer_order))
25 offset_b = integer_order / 2;
28 ## When the order is odd, we need one more "forward" point than we
29 ## do "backward" points.
30 offset_b = (integer_order - 1) / 2;
31 offset_f = offset_b + 1;
34 backward_xs = [x-(offset_b*h) : h : x];
36 ## We'll always have at least one forward point, so start this vector
37 ## from (x + h) and include `x` itself in the backward points.
38 forward_xs = [x+h : h : x+(offset_f*h)];
40 xs = horzcat(backward_xs, forward_xs);
42 ## Now that we have all of the points that we need in xs, we can use
43 ## the divided_difference function to compute the derivative.
44 df = divided_difference(f, xs);