]> gitweb.michael.orlitzky.com - octave.git/blob - forward_euler.m
c6ef21722dbbd3fa1c405f2a594ad793d0c48f79
[octave.git] / forward_euler.m
1 function df = forward_euler(integer_order, h, f, x)
2 ##
3 ## Use the forward Euler method to compute the derivative of `f` at
4 ## a point `x`.
5 ##
6 ## INPUTS:
7 ##
8 ## * ``integer_order`` - The order of the derivative.
9 ##
10 ## * ``h`` - The step size.
11 ##
12 ## * ``f`` - The function whose derivative we're computing.
13 ##
14 ## * ``x`` - The point at which to compute the derivative.
15 ##
16
17 if (integer_order == 0)
18 df = x;
19 return;
20 end
21
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;
26 offset_f = offset_b;
27 else
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;
32 end
33
34 backward_xs = [x-(offset_b*h) : h : x];
35
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)];
39
40 xs = horzcat(backward_xs, forward_xs);
41
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);
45 end