Newton's method is used to solve a system of equations f(x)=0 (here x is a d-dimensional vector and f is a d-dimensional vector of functions). There are 3 functions below:
IN THE FILE f.m --------------- function a=f(x) a=zeros(2,1); a(1)=x(1)*(x(1)*x(1)-3*x(2)*x(2))-1; a(2)=x(2)*(3*x(1)*x(1)-x(2)*x(2)); IN THE FILE newtstep.m ---------------------- function a=newtstep(x,h) n=size(x,1); // get the dimension of the COLUMN vector submitted F=f(x); // compute f(x) M=zeros(n,n); // prepare to make the matrix of derivatives for i=1:n v=zeros(n,1); v(i)=h; M(:,i)=(f(x+v)-F)/h; end a=x-M\F; // do the Newton step IN THE FILE newt.m ------------------ function y=newt(x,h,epsilon) y=x; z=norm(f(y)); // stop when this is below epsilon while z>epsilon y=newtstep(y,h); z=norm(f(y)); end
< M A T L A B > Copyright 1984-1999 The MathWorks, Inc. Version 5.3.1.29215a (R11.1) Oct 6 1999 To get started, type one of these: helpwin, helpdesk, or demo. For product information, type tour or visit www.mathworks.com. >> newt([1.5;0.5],1e-6,1e-6) ans = 1.0000 -0.0000 >> newt([-1.0;0.8],1e-6,1e-6) ans = -0.5000 0.8660 >> newt([-1.0;-0.8],1e-6,1e-6) ans = -0.5000 -0.8660 >>
Back to main numerical methods course page
Back to my main teaching page
Back to my main page