Numerical Analysis Lab Note #8 Newton's Method For Nonlinear System

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Numerical Analysis Lab Note #8

Newton’s Method for Nonlinear System

1. Implement the algorithm of Newton’s Method for Nonlinear Systems:


Input: function F (x) = [f1 (x), . . . , fn (x)]T , Jacobian Matrix function J(x) =
∂fi
( ∂xj
(x))1≤i,j≤n .
Output: approximation solution x = (x1 , . . . , xn ) for the nonlinear system F (x) =
0.

(1) Set T OL = 10−5 ; M axIter = 100; Iter = 1;


Set x = zeros(n, 1); %% Set inital guess x to be (0, . . . , 0).
(2) while Iter < M axIter do
Solve the n × n linear system J(x)y = −F (x).
Set x = x + y;
if kyk < T OL then break; end if
Set Iter = Iter + 1;
end while
(3) If Iter ≥ M axIter then
disp(‘Maximum number of iterations exceeded!’);
End if

2. Let x = (x1 , x2 ) and F (x1 , x2 ) = [f1 (x), f2 (x)]T be given by

f1 (x1 , x2 ) = x21 − x22 + 2x2 ;


f2 (x1 , x2 ) = 2x1 + x22 − 6.

Use the above algorithm to find an approximation x = (x1 , x2 ) for F (x) = 0.

3. MATLAB CODE:

% solution for Part 1.


function x = NewtonMethod(funcF,JacobianF,n)

F = funcF;
J = JacobianF;
x = zeros(n,1); % set initial to (0,...,0)
Iter = 1;
MaxIter = 100;
TOL = 1e-5;

1
while Iter < MaxIter
disp([’Iter = ’ num2str(Iter)]);
y = J(x)\(-F(x));
x = x+y;
if norm(y,2)<TOL break; end
disp([’x = ’ num2str(x’)];
end
if Iter >= MaxIter
disp(’Maximum number of iteration exceeded!’);
end
end

===========================================================
% solution for Part 2.
% function F
function y = F(x)
x1 = x(1);
x2 = x(2);
y = zeros(2,1);
y(1) = x1^2-x2^2+2*x2; % f1(x1,x2)
y(2) = 2*x1+x2^2-6; % f2(x1,x2);
end

% Jacobian matrix of F
function A = J(x)
x1 = x(1);
x2 = x(2);
A = zeros(2,2);
A(1,1) = 2*x1; % df1x1
A(1,2) = -2*x2+2; % df1x2
A(2,1) = 2; % df2x1
A(2,2) = 2*x2; % df2x2;
end

function newtonSol
x = NewtonMethod(@F,@J,2);
end

You might also like