You are on page 1of 5

This figure shows that the number of iterations required to achieve a solution ranges

from 0 to about 15. Notice also that, the closer the initial guess is to zero, the less
number of iterations to convergence are required.

NOTE: If the function of interest is defined by an m-file, the reference to the function
name in the call to newton.m should be placed between quotes.

Solving systems of non-linear equations

Consider the solution to a system of n non-linear equations in n unknowns given by

f1(x1,x2,,xn) = 0
f2(x1,x2,,xn) = 0
.
.
.
fn(x1,x2,,xn) = 0

The system can be written in a single expression using vectors, i.e.,

f(x) = 0,

where the vector x contains the independent variables, and the vector f contains the
functions fi(x):
x1 f1 ( x1 , x 2 ,..., x n ) f1 (x)
x f ( x , x ,..., x ) f (x)
x = 2 , f ( x) = 2 1 2 n
= 2 .
M M M

xn f n ( x1 , x 2 ,..., x n ) f n (x)

Newton-Raphson method to solve systems of non-linear equations

A Newton-Raphson method for solving the system of linear equations requires the
evaluation of a matrix, known as the Jacobian of the system, which is defined as:

f1 / x1 f 1 / x 2 L f1 / x n
L f 2 / x n
( f 1 , f 2 ,..., f n ) f 2 / x1 f 2 / x 2 f
J= = = [ i ] nn .
( x1 , x 2 ,..., x n ) M M O M x j

f n / x1 f n / x 2 L f n / x n

If x = x0 (a vector) represents the first guess for the solution, successive approximations
to the solution are obtained from
xn+1 = xn - J-1f(xn) = xn - xn,
with xn = xn+1 - xn.

9
A convergence criterion for the solution of a system of non-linear equation could be, for
example, that the maximum of the absolute values of the functions fi(xn) is smaller than a
certain tolerance , i.e.,
max | f i (x n ) |< .
i

Another possibility for convergence is that the magnitude of the vector f(xn) be smaller
than the tolerance, i.e.,
|f(xn)| < .

We can also use as convergence criteria the difference between consecutive values of the
solution, i.e.,
max | ( xi ) n +1 ( xi ) n | < .,
i
or,
|xn | = |xn+1 - xn| < .

The main complication with using Newton-Raphson to solve a system of non-linear


equations is having to define all the functions fi/xj, for i,j = 1,2, , n, included in the
Jacobian. As the number of equations and unknowns, n, increases, so does the number
of elements in the Jacobian, n2.

MATLAB function for Newton-Raphson method for a system of non-linear equations

The following MATLAB function, newtonm, calculates the solution to a system of n non-
linear equations, f(x) = 0, given the vector of functions f and the Jacobian J, as well as an
initial guess for the solution x0.

function [x,iter] = newtonm(x0,f,J)

% Newton-Raphson method applied to a


% system of linear equations f(x) = 0,
% given the jacobian function J, with
% J = del(f1,f2,...,fn)/del(x1,x2,...,xn)
% x = [x1;x2;...;xn], f = [f1;f2;...;fn]
% x0 is an initial guess of the solution

N = 100; % define max. number of iterations


epsilon = 1e-10; % define tolerance
maxval = 10000.0; % define value for divergence
xx = x0; % load initial guess

while (N>0)
JJ = feval(J,xx);
if abs(det(JJ))<epsilon
error('newtonm - Jacobian is singular - try new x0');
abort;
end;
xn = xx - inv(JJ)*feval(f,xx);

10
if abs(feval(f,xn))<epsilon
x=xn;
iter = 100-N;
return;
end;
if abs(feval(f,xx))>maxval
iter = 100-N;
disp(['iterations = ',num2str(iter)]);
error('Solution diverges');
abort;
end;
N = N - 1;
xx = xn;
end;
error('No convergence after 100 iterations.');
abort;
% end function

The functions f and the Jacobian J need to be defined as separate functions. To illustrate
the definition of the functions consider the system of non-linear equations:

f1(x1,x2) = x12 + x22 - 50 = 0,


f2(x1,x2) = x1 x2 - 25 = 0,

whose Jacobian is

f1 f1
x x 2 2 x1 2 x2
J= 1 = .
f 2 f 2 x 2 x1
x1 x 2

We can define the function f as the following user-defined MATLAB function f2:

function [f] = f2(x)


% f2(x) = 0, with x = [x(1);x(2)]
% represents a system of 2 non-linear equations
f1 = x(1)^2 + x(2)^2 - 50;
f2 = x(1)*x(2) -25;
f = [f1;f2];
% end function

The corresponding Jacobian is calculated using the user-defined MATLAB function


jacob2x2:

function [J] = jacob2x2(x)


% Evaluates the Jacobian of a 2x2
% system of non-linear equations
J(1,1) = 2*x(1); J(1,2) = 2*x(2);
J(2,1) = x(2); J(2,2) = x(1);
% end function

11
Illustrating the Newton-Raphson algorithm for a system of two non-linear equations

Before using function newtonm, we will perform some step-by-step calculations to


illustrate the algorithm. We start by defining an initial guess for the solution as:

x0 = [2;1]

x0 =

2
1

Lets calculate the function f(x) at x = x0 to see how far we are from a solution:

f2(x0)

ans =

-45
-23

Obviously, the function f(x0) is far away from being zero. Thus, we proceed to calculate
a better approximation by calculating the Jacobian J(x0):

J0 = jacob2x2(x0)

J0 =

4 2
1 2

The new approximation to the solution, x1, is calculated as:

x1 = x0 - inv(J0)*f2(x0)

x1 =

9.3333
8.8333

Evaluating the functions at x1 produces:

f2(x1)

ans = 115.1389

Still far away from convergence. Lets calculate a new approximation, x2:

x2 = x1-inv(jacob2x2(x1))*f2(x1)

x2 =

6.0428
5.7928

12
Evaluating the functions at x2 indicates that the values of the functions are decreasing:

f2(x2)

ans =

20.0723
10.0049

A new approximation and the corresponding function evaluations are:

x3 = x2 - inv(jacob2x2(x2))*f2(x2)

x3 =

5.1337
5.0087

E f2(x3)

ans =

1.4414
0.7129

The functions are getting even smaller suggesting convergence towards a solution.

Solution using function newtonm

Next, we use function newtonm to solve the problem postulated earlier.

A call to the function using the values of x0, f2, and jacob2x2 is:

[x,iter] = newtonm(x0,'f2','jacob2x2')

x =

5.0000
5.0000

iter =

16

The result shows the number of iterations required for convergence (16) and the solution
found as x1 = 5.0000 and x2 = 5.000. Evaluating the functions for those solutions results
in:

f2(x)

ans =

1.0e-010 *
0.2910
-0.1455

13

You might also like