SlideShare a Scribd company logo
2
Most read
3
Most read
9
Most read
1
SSoolluuttiioonn ooff nnoonn--lliinneeaarr eeqquuaattiioonnss
By Gilberto E. Urroz, September 2004
In this document I present methods for the solution of single non-linear equations as well
as for systems of such equations.
SSoolluuttiioonn ooff aa ssiinnggllee nnoonn--lliinneeaarr eeqquuaattiioonn
Equations that can be cast in the form of a polynomial are referred to as algebraic
equations. Equations involving more complicated terms, such as trigonometric,
hyperbolic, exponential, or logarithmic functions are referred to as transcendental
equations. The methods presented in this section are numerical methods that can be
applied to the solution of such equations, to which we will refer, in general, as non-linear
equations. In general, we will we searching for one, or more, solutions to the equation,
f(x) = 0.
We will present the Newton-Raphson algorithm, and the secant method. In the secant
method we need to provide two initial values of x to get the algorithm started. In the
Newton-Raphson methods only one initial value is required.
Because the solution is not exact, the algorithms for any of the methods presented herein
will not provide the exact solution to the equation f(x) = 0, instead, we will stop the
algorithm when the equation is satisfied within an allowed tolerance or error, ε. In
mathematical terms this is expressed as
|f(xR)| < ε.
The value of x for which the non-linear equation f(x)=0 is satisfied, i.e., x = xR, will be
the solution, or root, to the equation within an error of ε units.
The Newton-Raphson method
Consider the Taylor-series expansion of the function f(x) about a value x = xo:
f(x)= f(xo)+f'(xo)(x-xo)+(f"(xo)/2!)(x-xo)2+….
Using only the first two terms of the expansion, a first approximation to the root of the
equation
f(x) = 0
can be obtained from
f(x) = 0 ≈ f(xo)+f'(xo)(x1 -xo)
2
Such approximation is given by,
x1 = xo - f(xo)/f'(xo).
The Newton-Raphson method consists in obtaining improved values of the approximate
root through the recurrent application of equation above. For example, the second and
third approximations to that root will be given by
x2 = x1 - f(x1)/f'(x1),
and
x3= x2 - f(x2)/f'(x2),
respectively.
This iterative procedure can be generalized by writing the following equation, where i
represents the iteration number:
xi+1 = xi - f(xi)/f'(xi).
After each iteration the program should check to see if the convergence condition,
namely,
|f(x i+1)|<ε,
is satisfied.
The figure below illustrates the way in which the solution is found by using the Newton-
Raphson method. Notice that the equation f(x) = 0 ≈ f(xo)+f'(xo)(x1 -xo) represents a
straight line tangent to the curve y = f(x) at x = xo. This line intersects the x-axis (i.e., y =
f(x) = 0) at the point x1 as given by x1 = xo - f(xo)/f'(xo). At that point we can construct
another straight line tangent to y = f(x) whose intersection with the x-axis is the new
approximation to the root of f(x) = 0, namely, x = x2. Proceeding with the iteration we
can see that the intersection of consecutive tangent lines with the x-axis approaches the
actual root relatively fast.
3
The Newton-Raphson method converges relatively fast for most functions regardless of
the initial value chosen. The main disadvantage is that you need to know not only the
function f(x), but also its derivative, f'(x), in order to achieve a solution. The secant
method, discussed in the following section, utilizes an approximation to the derivative,
thus obviating such requirement.
The programming algorithm of any of these methods must include the option of stopping
the program if the number of iterations grows too large. How large is large? That will
depend of the particular problem solved. However, any Newton-Raphson, or secant
method solution that takes more than 1000 iterations to converge is either ill-posed or
contains a logical error. Debugging of the program will be called for at this point by
changing the initial values provided to the program, or by checking the program's logic.
AA MMAATTLLAABB ffuunnccttiioonn ffoorr tthhee NNeewwttoonn--RRaapphhssoonn mmeetthhoodd
The function newton, listed below, implements the Newton-Raphson algorithm. It uses
as arguments an initial value and expressions for f(x) and f'(x).
function [x,iter]=newton(x0,f,fp)
% newton-raphson algorithm
N = 100; eps = 1.e-5; % define max. no. iterations and error
maxval = 10000.0; % define value for divergence
xx = x0;
while (N>0)
xn = xx-f(xx)/fp(xx);
if abs(f(xn))<eps
x=xn;iter=100-N;
return;
end;
if abs(f(xx))>maxval
disp(['iterations = ',num2str(iter)]);
error('Solution diverges');
break;
end;
N = N - 1;
xx = xn;
end;
error('No convergence');
break;
% end function
We will use the Newton-Raphson method to solve for the equation, f(x) = x3
-2x2
+1 = 0.
The following MATLAB commands define the function f001(x) and its derivative,
f01p(x):
»f001 = inline('x.^3-2*x.^2+1','x')
f001 =
Inline function:
f001(x) = x.^3-2*x.^2+1
4
» f01p = inline('3*x.^2-2','x')
f01p =
Inline function:
f01p(x) = 3*x.^2-2
To have an idea of the location of the roots of this polynomial we'll plot the function
using the following MATLAB commands:
» x = [-0.8:0.01:2.0]';y=f001(x);
» plot(x,y);xlabel('x');ylabel('f001(x)');
» grid on
-1 -0.5 0 0.5 1 1.5 2
-1
-0.5
0
0.5
1
x
f001(x)
We see that the function graph crosses the x-axis somewhere between –1.0 and –0.5,
close to 1.0, and between 1.5 and 2.0. To activate the function we could use, for
example, an initial value x0 = 2:
» [x,iterations] = newton(2,f001,f01p)
x = 1.6180
iterations = 39
The following command are aimed at obtaining vectors of the solutions provided by
function newton.m for f001(x)=0 for initial values in the vector x0 such that –20 < x0 <
20. The solutions found are stored in variable xs while the required number of iterations
is stored in variable is.
» x0 = [-20:0.5:20]; xs = []; is = [];
EDU» for i = 1:length(x0)
[xx,ii] = newton(x0(i),f001,f01p);
xs = [xs,xx]; is = [is,ii];
end
Plot of xs vs. x0, and of is vs. x0 are shown next:
5
» figure(1);plot(x0,xs,'o');hold;plot(x0,xs,'-');hold;
» title('Newton-Raphson solution');
» xlabel('initial guess, x0');ylabel('solution, xs');
-20 -15 -10 -5 0 5 10 15 20
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
Newton-Raphson solution
initial guess, x0
solution,xs
This figure shows that for initial guesses in the range –20 < xo<20, function newton.m
converges mainly to the solution x = 1.6180, with few instances converting to the
solutions x = -1 and x = 1.
» figure(2);plot(x0,is,'+'); hold;plot(x0,is,'-');hold;
» title('Newton-Raphson solution');
» xlabel('initial guess, x0');ylabel('iterations, is');
-20 -15 -10 -5 0 5 10 15 20
0
10
20
30
40
50
60
70
Newton-Raphson solution
initial guess, x0
iterations,is
This figure shows that the number of iterations required to achieve a solution ranges from
0 to about 65. Most of the time, about 50 iterations are required. The following example
shows a case in which the solution actually diverges:
6
» [x,iter] = newton(-20.75,f001,f01p)
iterations = 6
??? Error using ==> newton
Solution diverges
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.
The Secant Method
In the secant method, we replace the derivative f'(xi) in the Newton-Raphson method with
f'(xi) ≈ (f(xi) - f(xi-1))/(xi - xi-1).
With this replacement, the Newton-Raphson algorithm becomes
To get the method started we need two values of x, say xo and x1, to get the first
approximation to the solution, namely,
As with the Newton-Raphson method, the iteration is stopped when
|f(x i+1)|<ε.
Figure 4, below, illustrates the way that the secant method approximates the solution of
the equation f(x) = 0.
).(
)()(
)(
1
1
1 −
−
+ −⋅
−
−= ii
ii
i
ii xx
xfxf
xf
xx
).(
)()(
)(
01
1
1
12 xx
xfxf
xf
xx
o
−⋅
−
−=
7
AA MMAATTLLAABB ffuunnccttiioonn ffoorr tthhee sseeccaanntt mmeetthhoodd
The function secant, listed below, uses the secant method to solve for non-linear
equations. It requires two initial values and an expression for the function, f(x).
function [x,iter]=secant(x0,x00,f)
% newton-raphson algorithm
N = 100; eps = 1.e-5; % define max. no. iterations and error
maxval = 10000.0; % define value for divergence
xx1 = x0; xx2 = x00;
while N>0
gp = (f(xx2)-f(xx1))/(xx2-xx1);
xn = xx1-f(xx1)/gp;
if abs(f(xn))<eps
x=xn;
iter = 100-N;
return;
end;
if abs(f(xn))>maxval
iter=100-N;
disp(['iterations = ',num2str(iter)]);
error('Solution diverges');
abort;
end;
N = N - 1;
xx1 = xx2;
xx2 = xn;
end;
iter=100-N;
disp(['iterations = ',iter]);
error('No convergence');
abort;
% end function
AApppplliiccaattiioonn ooff sseeccaanntt mmeetthhoodd
We use the same function f001(x) = 0 presented earlier. The following commands call
the function secant.txt to obtain a solution to the equation:
» [x,iter] = secant(-10.0,-9.8,f001)
x = -0.6180
iter = 11
The following command are aimed at obtaining vectors of the solutions provided by
function Newton.m for f001(x)=0 for initial values in the vector x0 such that –20 < x0 <
20. The solutions found are stored in variable xs while the required number of iterations
is stored in variable is.
8
» x0 = [-20:0.5:20]; x00 = x0 + 0.5; xs = []; is = [];
» for i = 1:length(x0)
[xx,ii] = secant(x0(i),x00(i),f001);
xs = [xs, xx]; is = [is, ii];
end
Plot of xs vs. x0, and of is vs. x0 are shown next:
» figure(1);plot(x0,xs,'o');hold;plot(x0,xs,'-');hold;
» title('Secant method solution');
» xlabel('first initial guess, x0');ylabel('solution, xs');
-20 -15 -10 -5 0 5 10 15 20
-1
-0.5
0
0.5
1
1.5
2
Secant method solution
first initial guess, x0
solution,xs
This figure shows that for initial guesses in the range –20 < xo<20, function newton.m
converges to the three solutions. Notice that initial guesses in the range –20 < x0 < -1,
converge to x = - 0.6180; those in the range –1< x < 1, converge to x = 1; and, those in
the range 1<x<20, converge to x =1.6180.
» figure(2);plot(x0,is,'o');hold;plot(x0,is,'-');hold;
» xlabel('first initial guess, x0');ylabel('iterations, is');
» title('Secant method solution');
-20 -15 -10 -5 0 5 10 15 20
0
5
10
15
first initial guess, x0
iterations,is
Secant method solution
9
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.
SSoollvviinngg ssyysstteemmss ooff nnoonn--lliinneeaarr eeqquuaattiioonnss
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):
.
)(
)(
)(
),...,,(
),...,,(
),...,,(
)(, 2
1
21
212
211
2
1












=












=












=
x
x
x
xfx
nnn
n
n
n f
f
f
xxxf
xxxf
xxxf
x
x
x
MMM
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:
.][
///
///
///
),...,,(
),...,,(
21
22212
12111
21
21
nn
j
i
nnnn
n
n
n
n
x
f
xfxfxf
xfxfxf
xfxfxf
xxx
fff
×
∂
∂
=












∂∂∂∂∂∂
∂∂∂∂∂∂
∂∂∂∂∂∂
=
∂
∂
=
L
MOMM
L
L
J
If x = x0 (a vector) represents the first guess for the solution, successive approximations
to the solution are obtained from
xn+1 = xn - J-1
⋅f(xn) = xn - ∆xn,
with ∆xn = xn+1 - xn.
10
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 ε<ni
i
f x
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 1 ε<−+ nini
i
xx
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);
11
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) = x1
2
+ x2
2
- 50 = 0,
f2(x1,x2) = x1 ⋅ x2 - 25 = 0,
whose Jacobian is
.
22
12
21
2
2
1
2
2
1
1
1






=












∂
∂
∂
∂
∂
∂
∂
∂
=
xx
xx
x
f
x
f
x
f
x
f
J
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
12
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
Let’s 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. Let’s calculate a new approximation, x2:
» x2 = x1-inv(jacob2x2(x1))*f2(x1)
x2 =
6.0428
5.7928
13
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
14
The values of the functions are close enough to zero (error in the order of 10-11
).
“Secant” method to solve systems of non-linear equations
In this section we present a method for solving systems of non-linear equations through
the Newton-Raphson algorithm, namely, xn+1 = xn - J-1
⋅f(xn), but approximating the
Jacobian through finite differences. This approach is a generalization of the secant
method for a single non-linear equation. For that reason, we refer to the method applied
to a system of non-linear equations as a “secant” method, although the geometric origin
of the term not longer applies.
The “secant” method for a system of non-linear equations free us from having to define
the n2
functions necessary to define the Jacobian for a system of n equations. Instead, we
approximate the partial derivatives in the Jacobian with
,
),,,,,(),,,,,( 2121
x
xxxxfxxxxxf
x
f njinji
j
i
∆
−∆+
≈
∂
∂ LLLL
where ∆x is a small increment in the independent variables. Notice that ∂fi/∂xj represents
element Jij in the jacobian J = ∂(f1,f2,…,fn)/∂(x1,x2,…,xn).
To calculate the Jacobian we proceed by columns, i.e., column j of the Jacobian will be
calculated as shown in the function jacobFD (jacobian calculated through Finite
Differences) listed below:
function [J] = jacobFD(f,x,delx)
% Calculates the Jacobian of the
% system of non-linear equations:
% f(x) = 0, through finite differences.
% The Jacobian is built by columns
[m n] = size(x);
for j = 1:m
xx = x;
xx(j) = x(j) + delx;
J(:,j) = (f(xx)-f(x))/delx;
end;
% end function
Notice that for each column (i.e., each value of j) we define a variable xx which is first
made equal to x, and then the j-th element is incremented by delx, before calculating the
j-th column of the Jacobian, namely, J(:,j). This is the MATLAB implementation of the
finite difference approximation for the Jacobian elements Jij = ∂fi/∂xj as defined earlier.
15
Illustrating the “secant” algorithm for a system of two non-linear equations
To illustrate the application of the “secant” algorithm we use again the system of two
non-linear equations defined earlier through the function f2.
We choose an initial guess for the solution as x0 = [2;3], and an increment in the
independent variables of ∆x = 0.1:
x0 = [2;3]
x0 =
2
3
EDU» dx = 0.1
dx = 0.1000
Variable J0 will store the Jacobian corresponding to x0 calculated through finite
differences with the value of ∆x defined above:
» J0 = jacobFD('f2',x0,dx)
J0 =
4.1000 6.1000
3.0000 2.0000
A new estimate for the solution, namely, x1, is calculated using the Newton-Raphson
algorithm:
» x1 = x0 - inv(J0)*f2(x0)
x1 =
6.1485
6.2772
The finite-difference Jacobian corresponding to x1 gets stored in J1:
» J1 = jacobFD('f2',x1,dx)
J1 =
12.3970 12.6545
6.2772 6.1485
And a new approximation for the solution (x2) is calculated as:
» x2 = x1 - inv(J1)*f2(x1)
x2 =
4.6671
5.5784
16
The next two approximations to the solution (x3 and x4) are calculated without first
storing the corresponding finite-difference Jacobians:
» x3 = x2 - inv(jacobFD('f2',x2,dx))*f2(x2)
x3 =
4.7676
5.2365
EDU» x4 = x3 - inv(jacobFD('f2',x3,dx))*f2(x3)
x4 =
4.8826
5.1175
To check the value of the functions at x = x4 we use:
» f2(x4)
ans =
0.0278
-0.0137
The functions are close to zero, but not yet at an acceptable error (i.e., something in the
order of 10-6
). Therefore, we try one more approximation to the solution, i.e., x5:
» x5 = x4 - inv(jacobFD('f2',x4,dx))*f2(x4)
x5 =
4.9413
5.0587
The functions are even closer to zero than before, suggesting a convergence to a solution.
» f2(x5)
ans =
0.0069
-0.0034
MMAATTLLAABB ffuunnccttiioonn ffoorr ““sseeccaanntt”” mmeetthhoodd ttoo ssoollvvee ssyysstteemmss ooff nnoonn--lliinneeaarr eeqquuaattiioonnss
To make the process of achieving a solution automatic, we propose the following
MATLAB user -defined function, secantm:
function [x,iter] = secantm(x0,dx,f)
% Secant-type method applied to a
% system of linear equations f(x) = 0,
17
% given the jacobian function J, with
% The Jacobian built by columns.
% x = [x1;x2;...;xn], f = [f1;f2;...;fn]
% x0 is the initial guess of the solution
% dx is an increment in x1,x2,... variables
N = 100; % define max. number of iterations
epsilon = 1.0e-10; % define tolerance
maxval = 10000.0; % define value for divergence
if abs(dx)<epsilon
error('dx = 0, use different values');
break;
end;
xn = x0; % load initial guess
[n m] = size(x0);
while (N>0)
JJ = [1,2;2,3]; xx = zeros(n,1);
for j = 1:n % Estimating
xx = xn; % Jacobian by
xx(j) = xn(j) + dx; % finite
fxx = feval(f,xx);
fxn = feval(f,xn);
JJ(:,j) = (fxx-fxn)/dx; % differences
end; % by columns
if abs(det(JJ))<epsilon
error('newtonm - Jacobian is singular - try new x0,dx');
break;
end;
xnp1 = xn - inv(JJ)*fxn;
fnp1 = feval(f,xnp1);
if abs(fnp1)<epsilon
x=xnp1;
disp(['iterations: ', num2str(100-N)]);
return;
end;
if abs(fnp1)>maxval
disp(['iterations: ', num2str(100-N)]);
error('Solution diverges');
break;
end;
N = N - 1;
xn = xnp1;
end;
error('No convergence');
break;
% end function
18
SSoolluuttiioonn uussiinngg ffuunnccttiioonn sseeccaannttmm
To solve the system represented by function f2, we now use function secantm. The
following call to function secantm produces a solution after 18 iterations:
» [x,iter] = secantm(x0,dx,'f2')
iterations: 18
x =
5.0000
5.0000
iter =
18
Solving equations with Matlab function fzero
Matlab provides function fzero for the solution of single non-linear equations. Use
» help fzero
to obtain additional information on function fzero. Also, read Chapter 7 (Function
Functions) in the Using Matlab guide.
For the solution of systems of non-linear equations Matlab provides function fsolve as
part of the Optimization package. Since this is an add-on package, function fsolve is not
available in the student version of Matlab. If using the full version of Matlab, check the
help facility for function fsolve by using:
» help fsolve
If function fsolve is not available in the Matlab installation you are using, you can always
use function secantm (or newtonm) to solve systems of non-linear equations.

More Related Content

PPT
Numerical Analysis (Solution of Non-Linear Equations) part 2
PDF
Newton's Forward/Backward Difference Interpolation
PPT
Linear transformation.ppt
PPTX
Presentation on Solution to non linear equations
PPTX
newton raphson method
PDF
Chapter 4 solving systems of nonlinear equations
PPTX
Runge kutta
PPT
Numerical method
Numerical Analysis (Solution of Non-Linear Equations) part 2
Newton's Forward/Backward Difference Interpolation
Linear transformation.ppt
Presentation on Solution to non linear equations
newton raphson method
Chapter 4 solving systems of nonlinear equations
Runge kutta
Numerical method

What's hot (20)

PPT
Newton raphson method
PPTX
Application of interpolation and finite difference
PPTX
Newton’s Forward & backward interpolation
PPTX
Runge Kutta Method
PPTX
Runge-Kutta methods with examples
PPTX
Runge kutta method -by Prof.Prashant Goad(R.C.Patel Institute of Technology,...
PPTX
Laplace Transform and its applications
PPTX
Application of Laplace Transforme
PPTX
Laplace transforms
PPTX
Power method
PPTX
Interpolation and its applications
PPTX
Gram-Schmidt process
PDF
Fixed point iteration
PDF
Langrange Interpolation Polynomials
PPT
First order non-linear partial differential equation & its applications
PDF
Interpolation with Finite differences
PPTX
Fourier transforms
PPTX
Newton Raphson Method
PPT
Newton divided difference interpolation
PPTX
Series solution to ordinary differential equations
Newton raphson method
Application of interpolation and finite difference
Newton’s Forward & backward interpolation
Runge Kutta Method
Runge-Kutta methods with examples
Runge kutta method -by Prof.Prashant Goad(R.C.Patel Institute of Technology,...
Laplace Transform and its applications
Application of Laplace Transforme
Laplace transforms
Power method
Interpolation and its applications
Gram-Schmidt process
Fixed point iteration
Langrange Interpolation Polynomials
First order non-linear partial differential equation & its applications
Interpolation with Finite differences
Fourier transforms
Newton Raphson Method
Newton divided difference interpolation
Series solution to ordinary differential equations
Ad

Viewers also liked (20)

PPTX
ROOT OF NON-LINEAR EQUATIONS
PPT
bisection method
PPT
Numerical Analysis (Solution of Non-Linear Equations)
PDF
Roots of Nonlinear Equations - Open Methods
PPTX
Systems of equations
PPTX
Linear and non linear equation
PPTX
Graph theory
DOCX
University of duhok
PPTX
system of non-linear equation (linear algebra & vector calculus)
PDF
Dennis j e, schnabel b numerical methods for unconstrained optimization and n...
PDF
Notes 6-1
PDF
Chapter 3 roots of equations
PDF
metode iterasi Gauss seidel
PPT
Roots of equations
PDF
NUMERICAL METHODS
PPT
World+water+day PPT
PPT
World water day
PPT
Gauss sediel
PPT
System call
ROOT OF NON-LINEAR EQUATIONS
bisection method
Numerical Analysis (Solution of Non-Linear Equations)
Roots of Nonlinear Equations - Open Methods
Systems of equations
Linear and non linear equation
Graph theory
University of duhok
system of non-linear equation (linear algebra & vector calculus)
Dennis j e, schnabel b numerical methods for unconstrained optimization and n...
Notes 6-1
Chapter 3 roots of equations
metode iterasi Gauss seidel
Roots of equations
NUMERICAL METHODS
World+water+day PPT
World water day
Gauss sediel
System call
Ad

Similar to Solution of non-linear equations (20)

PDF
Numarical values
PDF
Numarical values highlighted
PPTX
Secant method
DOCX
Roots of equations
DOCX
Equations root
PPT
Newton raphson method
PDF
Mit18 330 s12_chapter4
PPTX
Newton raphsonmethod presentation
PDF
Lecture 04 newton-raphson, secant method etc
PPT
Fourier series
PPTX
Newton Raphson
PDF
S3-3.pdf
PDF
Lecture6
DOCX
PDF
OPERATIONS RESEARCH
PDF
Fractional Newton-Raphson Method and Some Variants for the Solution of Nonlin...
PDF
Solutions for Problems: Engineering Optimization by Ranjan Ganguli
PPTX
CALCULUS 2.pptx
PPT
Newton-Raphson Iteration marths 4 ntsm
PDF
false-point.pdf
Numarical values
Numarical values highlighted
Secant method
Roots of equations
Equations root
Newton raphson method
Mit18 330 s12_chapter4
Newton raphsonmethod presentation
Lecture 04 newton-raphson, secant method etc
Fourier series
Newton Raphson
S3-3.pdf
Lecture6
OPERATIONS RESEARCH
Fractional Newton-Raphson Method and Some Variants for the Solution of Nonlin...
Solutions for Problems: Engineering Optimization by Ranjan Ganguli
CALCULUS 2.pptx
Newton-Raphson Iteration marths 4 ntsm
false-point.pdf

More from ZunAib Ali (20)

PPTX
Lcl filter design
PDF
Power converter report
PDF
Performance of Six-Pulse Line-Commutated Converter in DC Motor Drive Application
PPTX
SVM Simulation for three level inverter
PPTX
Multi phase Star Rectifier
PDF
Space vector pwm_inverter
PPT
Power transformer
PDF
Neutral point clamped inverter
DOCX
7 channel Interleaved Boost Converter
PPTX
Sinusoidal PWM and Space Vector Modulation For Two Level Voltage Source Conve...
PPTX
Rectifiers Simulation
DOCX
Instrumentational Amplifier
DOCX
Electronic Device pakages
PPTX
Inverter
PPTX
Concept of energy transmission & distribution
PPTX
DC Motor Model
PPTX
High Voltage Dc (HVDC) transmission
PDF
Cambridge ielts 9 full
PDF
Water level buzzer
PDF
Fourier Specturm via MATLAB
Lcl filter design
Power converter report
Performance of Six-Pulse Line-Commutated Converter in DC Motor Drive Application
SVM Simulation for three level inverter
Multi phase Star Rectifier
Space vector pwm_inverter
Power transformer
Neutral point clamped inverter
7 channel Interleaved Boost Converter
Sinusoidal PWM and Space Vector Modulation For Two Level Voltage Source Conve...
Rectifiers Simulation
Instrumentational Amplifier
Electronic Device pakages
Inverter
Concept of energy transmission & distribution
DC Motor Model
High Voltage Dc (HVDC) transmission
Cambridge ielts 9 full
Water level buzzer
Fourier Specturm via MATLAB

Recently uploaded (20)

PPTX
Institutional Correction lecture only . . .
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
RMMM.pdf make it easy to upload and study
PDF
Basic Mud Logging Guide for educational purpose
PDF
01-Introduction-to-Information-Management.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Insiders guide to clinical Medicine.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
PPH.pptx obstetrics and gynecology in nursing
Institutional Correction lecture only . . .
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
2.FourierTransform-ShortQuestionswithAnswers.pdf
Complications of Minimal Access Surgery at WLH
STATICS OF THE RIGID BODIES Hibbelers.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Anesthesia in Laparoscopic Surgery in India
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Computing-Curriculum for Schools in Ghana
Microbial diseases, their pathogenesis and prophylaxis
RMMM.pdf make it easy to upload and study
Basic Mud Logging Guide for educational purpose
01-Introduction-to-Information-Management.pdf
VCE English Exam - Section C Student Revision Booklet
Microbial disease of the cardiovascular and lymphatic systems
Insiders guide to clinical Medicine.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPH.pptx obstetrics and gynecology in nursing

Solution of non-linear equations

  • 1. 1 SSoolluuttiioonn ooff nnoonn--lliinneeaarr eeqquuaattiioonnss By Gilberto E. Urroz, September 2004 In this document I present methods for the solution of single non-linear equations as well as for systems of such equations. SSoolluuttiioonn ooff aa ssiinnggllee nnoonn--lliinneeaarr eeqquuaattiioonn Equations that can be cast in the form of a polynomial are referred to as algebraic equations. Equations involving more complicated terms, such as trigonometric, hyperbolic, exponential, or logarithmic functions are referred to as transcendental equations. The methods presented in this section are numerical methods that can be applied to the solution of such equations, to which we will refer, in general, as non-linear equations. In general, we will we searching for one, or more, solutions to the equation, f(x) = 0. We will present the Newton-Raphson algorithm, and the secant method. In the secant method we need to provide two initial values of x to get the algorithm started. In the Newton-Raphson methods only one initial value is required. Because the solution is not exact, the algorithms for any of the methods presented herein will not provide the exact solution to the equation f(x) = 0, instead, we will stop the algorithm when the equation is satisfied within an allowed tolerance or error, ε. In mathematical terms this is expressed as |f(xR)| < ε. The value of x for which the non-linear equation f(x)=0 is satisfied, i.e., x = xR, will be the solution, or root, to the equation within an error of ε units. The Newton-Raphson method Consider the Taylor-series expansion of the function f(x) about a value x = xo: f(x)= f(xo)+f'(xo)(x-xo)+(f"(xo)/2!)(x-xo)2+…. Using only the first two terms of the expansion, a first approximation to the root of the equation f(x) = 0 can be obtained from f(x) = 0 ≈ f(xo)+f'(xo)(x1 -xo)
  • 2. 2 Such approximation is given by, x1 = xo - f(xo)/f'(xo). The Newton-Raphson method consists in obtaining improved values of the approximate root through the recurrent application of equation above. For example, the second and third approximations to that root will be given by x2 = x1 - f(x1)/f'(x1), and x3= x2 - f(x2)/f'(x2), respectively. This iterative procedure can be generalized by writing the following equation, where i represents the iteration number: xi+1 = xi - f(xi)/f'(xi). After each iteration the program should check to see if the convergence condition, namely, |f(x i+1)|<ε, is satisfied. The figure below illustrates the way in which the solution is found by using the Newton- Raphson method. Notice that the equation f(x) = 0 ≈ f(xo)+f'(xo)(x1 -xo) represents a straight line tangent to the curve y = f(x) at x = xo. This line intersects the x-axis (i.e., y = f(x) = 0) at the point x1 as given by x1 = xo - f(xo)/f'(xo). At that point we can construct another straight line tangent to y = f(x) whose intersection with the x-axis is the new approximation to the root of f(x) = 0, namely, x = x2. Proceeding with the iteration we can see that the intersection of consecutive tangent lines with the x-axis approaches the actual root relatively fast.
  • 3. 3 The Newton-Raphson method converges relatively fast for most functions regardless of the initial value chosen. The main disadvantage is that you need to know not only the function f(x), but also its derivative, f'(x), in order to achieve a solution. The secant method, discussed in the following section, utilizes an approximation to the derivative, thus obviating such requirement. The programming algorithm of any of these methods must include the option of stopping the program if the number of iterations grows too large. How large is large? That will depend of the particular problem solved. However, any Newton-Raphson, or secant method solution that takes more than 1000 iterations to converge is either ill-posed or contains a logical error. Debugging of the program will be called for at this point by changing the initial values provided to the program, or by checking the program's logic. AA MMAATTLLAABB ffuunnccttiioonn ffoorr tthhee NNeewwttoonn--RRaapphhssoonn mmeetthhoodd The function newton, listed below, implements the Newton-Raphson algorithm. It uses as arguments an initial value and expressions for f(x) and f'(x). function [x,iter]=newton(x0,f,fp) % newton-raphson algorithm N = 100; eps = 1.e-5; % define max. no. iterations and error maxval = 10000.0; % define value for divergence xx = x0; while (N>0) xn = xx-f(xx)/fp(xx); if abs(f(xn))<eps x=xn;iter=100-N; return; end; if abs(f(xx))>maxval disp(['iterations = ',num2str(iter)]); error('Solution diverges'); break; end; N = N - 1; xx = xn; end; error('No convergence'); break; % end function We will use the Newton-Raphson method to solve for the equation, f(x) = x3 -2x2 +1 = 0. The following MATLAB commands define the function f001(x) and its derivative, f01p(x): »f001 = inline('x.^3-2*x.^2+1','x') f001 = Inline function: f001(x) = x.^3-2*x.^2+1
  • 4. 4 » f01p = inline('3*x.^2-2','x') f01p = Inline function: f01p(x) = 3*x.^2-2 To have an idea of the location of the roots of this polynomial we'll plot the function using the following MATLAB commands: » x = [-0.8:0.01:2.0]';y=f001(x); » plot(x,y);xlabel('x');ylabel('f001(x)'); » grid on -1 -0.5 0 0.5 1 1.5 2 -1 -0.5 0 0.5 1 x f001(x) We see that the function graph crosses the x-axis somewhere between –1.0 and –0.5, close to 1.0, and between 1.5 and 2.0. To activate the function we could use, for example, an initial value x0 = 2: » [x,iterations] = newton(2,f001,f01p) x = 1.6180 iterations = 39 The following command are aimed at obtaining vectors of the solutions provided by function newton.m for f001(x)=0 for initial values in the vector x0 such that –20 < x0 < 20. The solutions found are stored in variable xs while the required number of iterations is stored in variable is. » x0 = [-20:0.5:20]; xs = []; is = []; EDU» for i = 1:length(x0) [xx,ii] = newton(x0(i),f001,f01p); xs = [xs,xx]; is = [is,ii]; end Plot of xs vs. x0, and of is vs. x0 are shown next:
  • 5. 5 » figure(1);plot(x0,xs,'o');hold;plot(x0,xs,'-');hold; » title('Newton-Raphson solution'); » xlabel('initial guess, x0');ylabel('solution, xs'); -20 -15 -10 -5 0 5 10 15 20 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 Newton-Raphson solution initial guess, x0 solution,xs This figure shows that for initial guesses in the range –20 < xo<20, function newton.m converges mainly to the solution x = 1.6180, with few instances converting to the solutions x = -1 and x = 1. » figure(2);plot(x0,is,'+'); hold;plot(x0,is,'-');hold; » title('Newton-Raphson solution'); » xlabel('initial guess, x0');ylabel('iterations, is'); -20 -15 -10 -5 0 5 10 15 20 0 10 20 30 40 50 60 70 Newton-Raphson solution initial guess, x0 iterations,is This figure shows that the number of iterations required to achieve a solution ranges from 0 to about 65. Most of the time, about 50 iterations are required. The following example shows a case in which the solution actually diverges:
  • 6. 6 » [x,iter] = newton(-20.75,f001,f01p) iterations = 6 ??? Error using ==> newton Solution diverges 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. The Secant Method In the secant method, we replace the derivative f'(xi) in the Newton-Raphson method with f'(xi) ≈ (f(xi) - f(xi-1))/(xi - xi-1). With this replacement, the Newton-Raphson algorithm becomes To get the method started we need two values of x, say xo and x1, to get the first approximation to the solution, namely, As with the Newton-Raphson method, the iteration is stopped when |f(x i+1)|<ε. Figure 4, below, illustrates the way that the secant method approximates the solution of the equation f(x) = 0. ).( )()( )( 1 1 1 − − + −⋅ − −= ii ii i ii xx xfxf xf xx ).( )()( )( 01 1 1 12 xx xfxf xf xx o −⋅ − −=
  • 7. 7 AA MMAATTLLAABB ffuunnccttiioonn ffoorr tthhee sseeccaanntt mmeetthhoodd The function secant, listed below, uses the secant method to solve for non-linear equations. It requires two initial values and an expression for the function, f(x). function [x,iter]=secant(x0,x00,f) % newton-raphson algorithm N = 100; eps = 1.e-5; % define max. no. iterations and error maxval = 10000.0; % define value for divergence xx1 = x0; xx2 = x00; while N>0 gp = (f(xx2)-f(xx1))/(xx2-xx1); xn = xx1-f(xx1)/gp; if abs(f(xn))<eps x=xn; iter = 100-N; return; end; if abs(f(xn))>maxval iter=100-N; disp(['iterations = ',num2str(iter)]); error('Solution diverges'); abort; end; N = N - 1; xx1 = xx2; xx2 = xn; end; iter=100-N; disp(['iterations = ',iter]); error('No convergence'); abort; % end function AApppplliiccaattiioonn ooff sseeccaanntt mmeetthhoodd We use the same function f001(x) = 0 presented earlier. The following commands call the function secant.txt to obtain a solution to the equation: » [x,iter] = secant(-10.0,-9.8,f001) x = -0.6180 iter = 11 The following command are aimed at obtaining vectors of the solutions provided by function Newton.m for f001(x)=0 for initial values in the vector x0 such that –20 < x0 < 20. The solutions found are stored in variable xs while the required number of iterations is stored in variable is.
  • 8. 8 » x0 = [-20:0.5:20]; x00 = x0 + 0.5; xs = []; is = []; » for i = 1:length(x0) [xx,ii] = secant(x0(i),x00(i),f001); xs = [xs, xx]; is = [is, ii]; end Plot of xs vs. x0, and of is vs. x0 are shown next: » figure(1);plot(x0,xs,'o');hold;plot(x0,xs,'-');hold; » title('Secant method solution'); » xlabel('first initial guess, x0');ylabel('solution, xs'); -20 -15 -10 -5 0 5 10 15 20 -1 -0.5 0 0.5 1 1.5 2 Secant method solution first initial guess, x0 solution,xs This figure shows that for initial guesses in the range –20 < xo<20, function newton.m converges to the three solutions. Notice that initial guesses in the range –20 < x0 < -1, converge to x = - 0.6180; those in the range –1< x < 1, converge to x = 1; and, those in the range 1<x<20, converge to x =1.6180. » figure(2);plot(x0,is,'o');hold;plot(x0,is,'-');hold; » xlabel('first initial guess, x0');ylabel('iterations, is'); » title('Secant method solution'); -20 -15 -10 -5 0 5 10 15 20 0 5 10 15 first initial guess, x0 iterations,is Secant method solution
  • 9. 9 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. SSoollvviinngg ssyysstteemmss ooff nnoonn--lliinneeaarr eeqquuaattiioonnss 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): . )( )( )( ),...,,( ),...,,( ),...,,( )(, 2 1 21 212 211 2 1             =             =             = x x x xfx nnn n n n f f f xxxf xxxf xxxf x x x MMM 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: .][ /// /// /// ),...,,( ),...,,( 21 22212 12111 21 21 nn j i nnnn n n n n x f xfxfxf xfxfxf xfxfxf xxx fff × ∂ ∂ =             ∂∂∂∂∂∂ ∂∂∂∂∂∂ ∂∂∂∂∂∂ = ∂ ∂ = L MOMM L L J If x = x0 (a vector) represents the first guess for the solution, successive approximations to the solution are obtained from xn+1 = xn - J-1 ⋅f(xn) = xn - ∆xn, with ∆xn = xn+1 - xn.
  • 10. 10 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 ε<ni i f x 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 1 ε<−+ nini i xx 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);
  • 11. 11 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) = x1 2 + x2 2 - 50 = 0, f2(x1,x2) = x1 ⋅ x2 - 25 = 0, whose Jacobian is . 22 12 21 2 2 1 2 2 1 1 1       =             ∂ ∂ ∂ ∂ ∂ ∂ ∂ ∂ = xx xx x f x f x f x f J 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
  • 12. 12 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 Let’s 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. Let’s calculate a new approximation, x2: » x2 = x1-inv(jacob2x2(x1))*f2(x1) x2 = 6.0428 5.7928
  • 13. 13 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
  • 14. 14 The values of the functions are close enough to zero (error in the order of 10-11 ). “Secant” method to solve systems of non-linear equations In this section we present a method for solving systems of non-linear equations through the Newton-Raphson algorithm, namely, xn+1 = xn - J-1 ⋅f(xn), but approximating the Jacobian through finite differences. This approach is a generalization of the secant method for a single non-linear equation. For that reason, we refer to the method applied to a system of non-linear equations as a “secant” method, although the geometric origin of the term not longer applies. The “secant” method for a system of non-linear equations free us from having to define the n2 functions necessary to define the Jacobian for a system of n equations. Instead, we approximate the partial derivatives in the Jacobian with , ),,,,,(),,,,,( 2121 x xxxxfxxxxxf x f njinji j i ∆ −∆+ ≈ ∂ ∂ LLLL where ∆x is a small increment in the independent variables. Notice that ∂fi/∂xj represents element Jij in the jacobian J = ∂(f1,f2,…,fn)/∂(x1,x2,…,xn). To calculate the Jacobian we proceed by columns, i.e., column j of the Jacobian will be calculated as shown in the function jacobFD (jacobian calculated through Finite Differences) listed below: function [J] = jacobFD(f,x,delx) % Calculates the Jacobian of the % system of non-linear equations: % f(x) = 0, through finite differences. % The Jacobian is built by columns [m n] = size(x); for j = 1:m xx = x; xx(j) = x(j) + delx; J(:,j) = (f(xx)-f(x))/delx; end; % end function Notice that for each column (i.e., each value of j) we define a variable xx which is first made equal to x, and then the j-th element is incremented by delx, before calculating the j-th column of the Jacobian, namely, J(:,j). This is the MATLAB implementation of the finite difference approximation for the Jacobian elements Jij = ∂fi/∂xj as defined earlier.
  • 15. 15 Illustrating the “secant” algorithm for a system of two non-linear equations To illustrate the application of the “secant” algorithm we use again the system of two non-linear equations defined earlier through the function f2. We choose an initial guess for the solution as x0 = [2;3], and an increment in the independent variables of ∆x = 0.1: x0 = [2;3] x0 = 2 3 EDU» dx = 0.1 dx = 0.1000 Variable J0 will store the Jacobian corresponding to x0 calculated through finite differences with the value of ∆x defined above: » J0 = jacobFD('f2',x0,dx) J0 = 4.1000 6.1000 3.0000 2.0000 A new estimate for the solution, namely, x1, is calculated using the Newton-Raphson algorithm: » x1 = x0 - inv(J0)*f2(x0) x1 = 6.1485 6.2772 The finite-difference Jacobian corresponding to x1 gets stored in J1: » J1 = jacobFD('f2',x1,dx) J1 = 12.3970 12.6545 6.2772 6.1485 And a new approximation for the solution (x2) is calculated as: » x2 = x1 - inv(J1)*f2(x1) x2 = 4.6671 5.5784
  • 16. 16 The next two approximations to the solution (x3 and x4) are calculated without first storing the corresponding finite-difference Jacobians: » x3 = x2 - inv(jacobFD('f2',x2,dx))*f2(x2) x3 = 4.7676 5.2365 EDU» x4 = x3 - inv(jacobFD('f2',x3,dx))*f2(x3) x4 = 4.8826 5.1175 To check the value of the functions at x = x4 we use: » f2(x4) ans = 0.0278 -0.0137 The functions are close to zero, but not yet at an acceptable error (i.e., something in the order of 10-6 ). Therefore, we try one more approximation to the solution, i.e., x5: » x5 = x4 - inv(jacobFD('f2',x4,dx))*f2(x4) x5 = 4.9413 5.0587 The functions are even closer to zero than before, suggesting a convergence to a solution. » f2(x5) ans = 0.0069 -0.0034 MMAATTLLAABB ffuunnccttiioonn ffoorr ““sseeccaanntt”” mmeetthhoodd ttoo ssoollvvee ssyysstteemmss ooff nnoonn--lliinneeaarr eeqquuaattiioonnss To make the process of achieving a solution automatic, we propose the following MATLAB user -defined function, secantm: function [x,iter] = secantm(x0,dx,f) % Secant-type method applied to a % system of linear equations f(x) = 0,
  • 17. 17 % given the jacobian function J, with % The Jacobian built by columns. % x = [x1;x2;...;xn], f = [f1;f2;...;fn] % x0 is the initial guess of the solution % dx is an increment in x1,x2,... variables N = 100; % define max. number of iterations epsilon = 1.0e-10; % define tolerance maxval = 10000.0; % define value for divergence if abs(dx)<epsilon error('dx = 0, use different values'); break; end; xn = x0; % load initial guess [n m] = size(x0); while (N>0) JJ = [1,2;2,3]; xx = zeros(n,1); for j = 1:n % Estimating xx = xn; % Jacobian by xx(j) = xn(j) + dx; % finite fxx = feval(f,xx); fxn = feval(f,xn); JJ(:,j) = (fxx-fxn)/dx; % differences end; % by columns if abs(det(JJ))<epsilon error('newtonm - Jacobian is singular - try new x0,dx'); break; end; xnp1 = xn - inv(JJ)*fxn; fnp1 = feval(f,xnp1); if abs(fnp1)<epsilon x=xnp1; disp(['iterations: ', num2str(100-N)]); return; end; if abs(fnp1)>maxval disp(['iterations: ', num2str(100-N)]); error('Solution diverges'); break; end; N = N - 1; xn = xnp1; end; error('No convergence'); break; % end function
  • 18. 18 SSoolluuttiioonn uussiinngg ffuunnccttiioonn sseeccaannttmm To solve the system represented by function f2, we now use function secantm. The following call to function secantm produces a solution after 18 iterations: » [x,iter] = secantm(x0,dx,'f2') iterations: 18 x = 5.0000 5.0000 iter = 18 Solving equations with Matlab function fzero Matlab provides function fzero for the solution of single non-linear equations. Use » help fzero to obtain additional information on function fzero. Also, read Chapter 7 (Function Functions) in the Using Matlab guide. For the solution of systems of non-linear equations Matlab provides function fsolve as part of the Optimization package. Since this is an add-on package, function fsolve is not available in the student version of Matlab. If using the full version of Matlab, check the help facility for function fsolve by using: » help fsolve If function fsolve is not available in the Matlab installation you are using, you can always use function secantm (or newtonm) to solve systems of non-linear equations.