Nonlinear Equations Matlab
Nonlinear Equations Matlab
In this document I present methods for the solution of single non-linear equations as well
as for systems of such equations.
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.
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
1
Such approximation is given by,
x1 = xo - f(xo)/f'(xo).
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.
2
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.
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
3
» 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
0.5
f001(x)
-0.5
-1
-1 -0.5 0 0.5 1 1.5 2
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.
4
» figure(1);plot(x0,xs,'o');hold;plot(x0,xs,'-');hold;
» title('Newton-Raphson solution');
» xlabel('initial guess, x0');ylabel('solution, xs');
Newton-Raphson solution
1.8
1.7
1.6
1.5
solution, xs
1.4
1.3
1.2
1.1
1
-20 -15 -10 -5 0 5 10 15 20
initial guess, x0
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');
Newton-Raphson solution
70
60
50
iterations, is
40
30
20
10
0
-20 -15 -10 -5 0 5 10 15 20
initial guess, x0
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:
5
» [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.
In the secant method, we replace the derivative f'(xi) in the Newton-Raphson method with
f ( xi )
xi +1 = xi − ⋅ ( xi − xi −1 ).
f ( xi ) − f ( xi −1 )
To get the method started we need two values of x, say xo and x1, to get the first
approximation to the solution, namely,
f ( x1 )
x 2 = x1 − ⋅ ( x1 − x 0 ).
f ( x1 ) − f ( xo )
|f(x i+1)|<ε.
Figure 4, below, illustrates the way that the secant method approximates the solution of
the equation f(x) = 0.
6
A MATLAB function for the secant method
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
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.
7
» 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
1.5
1
solution, xs
0.5
-0.5
-1
-20 -15 -10 -5 0 5 10 15 20
first initial guess, x0
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');
10
iterations, is
0
-20 -15 -10 -5 0 5 10 15 20
first initial guess, x0
8
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.
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:
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.
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
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
» x1 = x0 - inv(J0)*f2(x0)
x1 =
9.3333
8.8333
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
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
The values of the functions are close enough to zero (error in the order of 10-11).
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
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:
[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.
14
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
» 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
» J1 = jacobFD('f2',x1,dx)
J1 =
12.3970 12.6545
6.2772 6.1485
» x2 = x1 - inv(J1)*f2(x1)
x2 =
4.6671
5.5784
15
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
» 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
16
% 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
if abs(dx)<epsilon
error('dx = 0, use different values');
break;
end;
[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
17
Solution using function secantm
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
Matlab provides function fzero for the solution of single non-linear equations. Use
» help fzero
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.
18