Professional Documents
Culture Documents
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.
f1(x1,x2,,xn) = 0
f2(x1,x2,,xn) = 0
.
.
.
fn(x1,x2,,xn) = 0
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)
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 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.
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:
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:
11
Illustrating the Newton-Raphson algorithm for a system of two non-linear equations
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
x1 = x0 - inv(J0)*f2(x0)
x1 =
9.3333
8.8333
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
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.
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