]> gitweb.michael.orlitzky.com - octave.git/commitdiff
Add the forward_euler1 function and a test for it.
authorMichael Orlitzky <michael@orlitzky.com>
Mon, 17 Dec 2012 15:19:15 +0000 (10:19 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 17 Dec 2012 15:19:15 +0000 (10:19 -0500)
forward_euler1.m [new file with mode: 0644]
run-tests.m

diff --git a/forward_euler1.m b/forward_euler1.m
new file mode 100644 (file)
index 0000000..3e1061d
--- /dev/null
@@ -0,0 +1,20 @@
+function y = forward_euler1(x0, y0, f, h)
+  ## Compute one iteration of the forward Euler method.
+  ##
+  ## INPUT:
+  ##
+  ##   * ``x0`` - The initial x-coordinate.
+  ##
+  ##   * ``y0`` - The initial value y(x0).
+  ##
+  ##   * ``f`` - The function y' = f(x,y) of two variables.
+  ##
+  ##   * ``h`` - The step size.
+  ##
+  ## OUTPUT:
+  ##
+  ## The approximate value of y(x) at x = x0+h.
+  ##
+  y_prime = f(x0,y0);
+  y = y0 +  h * y_prime;
+end
index 2892ba0e84f916571960177c62bb297068d7b448..2ff59a3e7081106154661b3b33b3c737e66e71c8 100755 (executable)
@@ -102,3 +102,16 @@ expected_root = [1.33635; 1.75424];
 unit_test_equals("Homework #3 problem #4 root is correct", ...
                 expected_root, ...
                 actual_root);
+
+
+
+f = @(x,y) y;
+x0 = 0;
+y0 = 1;
+h = 1;
+actual_y = forward_euler1(x0, y0, f, h);
+expected_y = 2;
+
+unit_test_equals("Forward Euler works for one step", ...
+                expected_y, ...
+                actual_y);