NEWTON PROGRAM
TO USE THE PROGRAM
Cut and paste the following mfile and put it where Matlab will find it.
Then look at the example at the bottom of the page.
NEWTON MFILE
--------------------
function[x] = newton(F, x0, iterations)
% Function NEWTON(F, x0, iterations)
% Estimate zeros of F by Newton's method.
% F should be a column vector n high of
% real-valued functions of n variables.
% These variables must be named x1 ... xn.
% x0 is an initial guess, a real column n-vector.
% iterations is the number of iterations to compute.
% EXAMPLE:
% newton([cos(x1)-x1; sin(x2)], [.1; 3.0], 3)
%
% Calculate the Jacobian matrix (derivative) symbolically.
vars = '[';
% construct an array of symbolic variable names for
% Jacobian
for i = 1:length(F)
iS = num2str(i);
vars = [vars 'x' iS ' '];
eval(['x' iS ' = sym(''x' iS ''');']);
end
vars = [vars ']' ];
eval(['vars= ' vars ';']);
J = jacobian(F, vars);
% call Maple routine
% Iterate.
x = x0;
for i = 1:iterations
JJ = double(subs(J, vars, x.'));
FF = double(subs(F, vars, x.'));
x = x - inv(JJ) * FF
end
----------------------
EXAMPLE
The following
two lines give an example of how to use this program.
EDU>syms x1 x2
EDU>newton([cos(x1)-x1; sin(x2)], [.1; 3.0], 3)
The first lists the variables; they
must be called x1, x2,... xn; n may be whatever you like.
Do not separate by commas; if n=3
write x1 x2 x3.
The second line contains the word newton and then various terms within
parentheses. These are the arguments of the function Newton. The first argument,
within the first square brackets, is the list of the functions
f_1 up to f_n that you are trying to set to 0. Of necessity this n is the same
n as for line one. Each f is a function of the n variables, or some
subset of the n variables. The second entry, in the second square brackets,
is the point at which to start Newton's method. The third entry is the number
of times to iterate. It is not
in brackets. The three entries are separated by commas.
Note: The semicolons separating the entries in the
first square brackets means that they are column vectors; this is Matlab's
convention for writing column vectors.
Use * to indicate multiplication, and ^ for power.
---
The Newton program was created by Jon Rosenberger when he ws an undergraduate at Cornell.