From: Michael Orlitzky Date: Mon, 17 Dec 2012 15:19:15 +0000 (-0500) Subject: Add the forward_euler1 function and a test for it. X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=24b6a4fe081d46f6a2f7ee2a3161401256d21fb5;p=octave.git Add the forward_euler1 function and a test for it. --- diff --git a/forward_euler1.m b/forward_euler1.m new file mode 100644 index 0000000..3e1061d --- /dev/null +++ b/forward_euler1.m @@ -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 diff --git a/run-tests.m b/run-tests.m index 2892ba0..2ff59a3 100755 --- a/run-tests.m +++ b/run-tests.m @@ -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);