]> gitweb.michael.orlitzky.com - octave.git/blob - tests/newtons_method_tests.m
Fix a comment.
[octave.git] / tests / newtons_method_tests.m
1 f = @(x) x^6 - x - 1;
2 f_prime = @(x) 6*x^5 - 1;
3 tol = 1/1000000;
4 x0 = 2;
5 expected_root = 1.1347;
6 unit_test_equals("Newton's method agrees with Haskell", ...
7 expected_root, ...
8 newtons_method(f, f_prime, tol, x0));
9
10
11 f1 = @(u) u(1)^2 + u(1)*u(2)^3 - 9;
12 f2 = @(u) 3*u(1)^2*u(2) - u(2)^3 - 4;
13 f = @(u) [f1(u); f2(u)];
14 ## The partials for the Jacobian.
15 f1x = @(u) 2*u(1) + u(2)^3;
16 f1y = @(u) 3*u(1)*u(2)^2;
17 f2x = @(u) 6*u(1)*u(2);
18 f2y = @(u) 3*u(1)^2 - 3*u(2)^2;
19 ## f_prime == Jacobian.
20 f_prime = @(u) [ f1x(u), f1y(u); f2x(u), f2y(u) ];
21 tol = 1 / 10^12;
22 u0 = [1.2; 2.5];
23 expected_root = [1.33635; 1.75424];
24 [actual_root, iterations] = newtons_method(f, f_prime, tol, u0);
25 unit_test_equals("Homework #3 problem #4 root is correct", ...
26 expected_root, ...
27 actual_root);