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:
function a=f(x) 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)); endfunction 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 endfunction 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 endfunction
=========== S c i l a b =========== scilab-2.6 Copyright (C) 1989-2001 INRIA Startup execution: loading initial environment -->exec newt; -->newt([1.5;0.5],1e-6,1e-6) ans = ! 1. ! ! - 1.203E-10 ! -->newt([-1.0;0.8],1e-6,1e-6) ans = ! - 0.5 ! ! 0.8660254 ! -->newt([-1.0;-0.8],1e-6,1e-6) ans = ! - 0.5 ! ! - 0.8660254 ! -->
Back to main numerical methods course page
Back to my main teaching page
Back to my main page