0% found this document useful (0 votes)
414 views38 pages

Numerical Solution of Ordinary Differential Equations Part 2 - Nonlinear Equations

This document discusses numerical methods for solving nonlinear equations that arise in engineering problems. It covers graphical and incremental search methods for finding the roots of nonlinear single-variable equations. Examples are provided of equations modeling parachutist velocity, reservoir area, and their solutions using MATLAB plots and the ginput command to locate the root graphically. Bracketing and open methods are also listed as available approaches for solving nonlinear equations numerically.

Uploaded by

Melih Tecer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
414 views38 pages

Numerical Solution of Ordinary Differential Equations Part 2 - Nonlinear Equations

This document discusses numerical methods for solving nonlinear equations that arise in engineering problems. It covers graphical and incremental search methods for finding the roots of nonlinear single-variable equations. Examples are provided of equations modeling parachutist velocity, reservoir area, and their solutions using MATLAB plots and the ginput command to locate the root graphically. Bracketing and open methods are also listed as available approaches for solving nonlinear equations numerically.

Uploaded by

Melih Tecer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

MIDDLE EAST TECHNICAL UNIVERSITY

FACULTY OF ENGINEERING
Department of Engineering Sciences

Course Code and Title:


ES 510 Numerical Solution of Ordinary Differential Equations (3-0) 3

Single Variable Nonlinear Equations

Mathematical modelling of some engineering problems yields single and/or set of nonlinear
equations solution of which in most of the cases cannot be obtained by analytical methods. We
will first deal with the solution of single variable nonlinear equations. We have already shown at
the beginning of the previous lecture that solution of nonlinear single variable equation becomes
necessity to determine the value of the unknown. In the following we have illustrated some
examples of nonlinear single variable equations.

3√𝑥 + 𝑥 = 32 , sin(2𝑥) − cos(𝑥) + 5𝑥 = 28 , 3𝑒 −2𝑥 + 𝑥 − 10 = 0,

𝑠𝑖𝑛2 (𝑥) − 0.2𝑥 − 1 = 0 , 𝑒 −0.2𝑥 sin(3𝑥 − 0.5) − ln(𝑥) − 16 = 0 , 3𝑥 5 + 2𝑥 2 + 𝑥 − 10 = 0

5 5
√(𝑥 − 31)/𝑥 + √𝑥 + 1 = 2

The values of 𝑥 that satisfies 𝑓(𝑥) = 0 are known as roots of the nonlinear equation. The number
of roots may be finite or infinite and may be real or complex depending on the nature of the
equation and the physical problem. The roots of nonlinear equation are also known as the zeros of
𝑓(𝑥) = 0 .

In page 53 to 58 of the textbook, there are examples of real engineering problems which require
determination of roots of nonlinear equations.

The available numerical methods for the solution of nonlinear equations can be collected in three
groups as listed below.

1. Graphical Method (incremental search method)


2. Bracketing Methods
3. Open Methods

1. Graphical methods (incremental search method)

Numerical methods are based on step by step calculations which intends to approach to the solution
numerically. This means large amount of arithmetic calculation which is manually tedious work.
However, presence of computers makes this work feasible because of their fast and correct
computation power. Therefore, what was not worth mentioning as a method now can be used to
solve nonlinear equations. Graphical solution method is not practical to be used manually.
However, in MATLAB environment it becomes feasible which do not need deep computer
programming knowledge. We will introduce this facility in the following.
1
Incremental search method

In this method the value of 𝑥 is incremented from an arbitrarily selected initial value, say 𝑥 = 𝑥1
successively until a change takes place in the sign of 𝑓(𝑥). The idea is that 𝑓(𝑥) changes sign
between 𝑥𝑖 and 𝑥𝑖+1if it has a root in the interval [𝑥𝑖 , 𝑥𝑖+1 ] as shown in the following figure. This
implies that 𝑓(𝑥𝑖 )𝑓(𝑥𝑖+1 ) < 0 whenever a root is crossed.

Following figure explains the steps of incremental search method.

2
Example 1:

Increasing accuracy:

Increasing accuracy further:

3
We could execute the above calculations in MATLAB by writing the following simple program.

x=0:0.1:0.7;
y=4.905*x.^2-15*x+5;
disp (' x y')
for i=1:length(x)
fprintf('%6.4f %10.4f\n', x(i), y(i))
end

The output will be as follows which the same as the first table above is:

x y
0.0000 5.0000
0.1000 3.5491
0.2000 2.1962
0.3000 0.9414
0.4000 -0.2152
0.5000 -1.2737
0.6000 -2.2342
0.7000 -3.0966

The other tables can be obtained in a similar manner by just changing the limits and increment
values in x array given in the first line of the program.

We can use plot function of MATLAB to carry out the following example in a shorter way.

x=0:0.01:1;
y=4.905*x.^2-15*x+5;
plot(x,y); grid

which yields to following graph.

By checking the point at which the graph intersects the x axis one can find the root. To get the
numerical value of the root ginput command can be used.

4
x=0:0.01:1;
y=4.905*x.^2-15*x+5;
plot(x,y); grid
ginput

ginput command brings two orthogonal lines on the graph. By coinciding this plus sign with the
intersection point and then pressing “enter” gives the values of the intersection point which is the
root on the command window of MATLAB as given below.

ans =

0.3821 -0.0140

What it gives is the coordinates of the intersection point. Because we are unable to coincide it
exactly on the intersection point, we get y value which is supposed to be zero. However, the x
value is the approximate root.

Example 2: Falling parachutist

Simple mathematical modelling of a falling parachutist shown in the figure below necessitates use
of Newton’s second law. 𝐹 = 𝑚𝑎 where F is the net force acting on the body, m is the mass of the
object and a is its acceleration. From which the acceleration can be expressed as 𝑎 = 𝐹/𝑚.
Noticing the fact that the acceleration is the change of the velocity with time, it follows

𝑑𝑣 𝐹
= where v is velocity and t is time. The net force on the body is 𝐹 = 𝐹𝐷 − 𝐹𝑈 in which
𝑑𝑡 𝑚

5
𝐹𝐷 = 𝑚𝑔 where g is gravitational acceleration. If the air resistance can
simply be written as proportional to the velocity, then 𝐹𝑈 = −𝑐𝑣 where
c is called drag coefficient. Substituting these expressions into the
𝑑𝑣 𝑚𝑔−𝑐𝑣
differential equation, it follows = which can be
𝑑𝑡 𝑚
simplified into

𝑑𝑣 𝑐
=𝑔− 𝑣
𝑑𝑡 𝑚

Solving this differential equation considering the initial condition of at


t=0 , v=0, the solution is obtained as

𝑔𝑚
𝑣(𝑡) = (1 − 𝑒 −𝑐𝑡/𝑚 ) (1)
𝑐

Which gives the velocity of the parachutist as function of time.

When we consider a parachutist of mass 68.1kg jumps out of stationary hot air balloon with drag
coefficient equal to 12.5 kg/s then the above expression takes the following form.

9.8 × 68.1
𝑣(𝑡) = (1 − 𝑒 −12.5𝑡/68.1 ) = 53.39(1 − 𝑒 −0.18355𝑡 )
12.5

Which can be used to compute the velocity at any time after the jump. This is the exact solution
of the above problem.

When the above problem is changed to calculate the drag coefficient c needed for a parachutist
of mass m=68.1kg to have a velocity of 40m/s after free-falling for time t=10s, we end up with
the following nonlinear equation. Going back to eqn. (1) and substituting the above values of
known parameters, it follows

9.8×68.1
40 = (1 − 𝑒 −10𝑡/68.1 )
𝑐
Which can be simplified to

667.38
𝑓(𝑐) = (1 − 𝑒−0.146843𝑐 ) − 40 = 0
𝑐

The root of the above equation gives the value of the drag coefficient. We can obtain the root by
using graphical solution. The following MATLAB code plots the variation of f(c) with respect to
t.
x=0.1:0.1:20;
y=667.8*(1-exp(-0.146843*x))./x-40;
plot(x,y)
grid
ginput
6
ans =

14.6777 -0.1051

Therefore, the value of drag coefficient should be 14.677 approximately.

Example 3:

1.1288
Determine the root of  tanh(1.3288 h ) by graphical method.
h

h=0.1:0.1:5;
sh=sqrt(h);
y1=1.1288./sh;
y2=tanh(1.3288*sh);
plot(h,y1,h,y2)
grid
ginput

7
y1, y2

1.1288
𝑦1 =
√ℎ

h=1.45m

𝑦2 = tanh(1.3288√ℎ)

Solution of reservoir problem

Example 4: Graphical Solution of Transcendental Equations

Transcendental equations are very common in engineering problems. ex –x = 0, in x –x = 0, sin x


+ cos x = 0 are some examples for transcendental equations. MATLAB can be used to solve these
equations graphically. Let us consider the following problem. The area of the segment enclosed
by the arc ACB and the chord AB is calculated from A = r2 ( - sin )/2. We can write the equation
as A/r2 = ( - sin )/2.

We can plot two curves y1 = ( - sin ())/2 and y2 = A/r2 and look
for their intersection. The following MATLAB statements will find
the value of Φ for r=2m, area=0.0472m2.

8
r=2; area=0.0472;
phi=0:pi/360:pi/4;
x=180*phi/pi;
y1=(phi-sin(phi))/2;
y2=area/r^2*ones(size(y1));
plot(x,y1,x,y2),grid
xlabel(' angle at center, phi, degrees')
gtext('(phi-sin(phi))/2')
gtext(' area/r^2')
[phi0 y0]= ginput(1)

9
BRACKETTING METHODS

Bi-section method:

10
11
12
Example:

13
MATLAB APPLICATION OF BI-SECTION METHOD

BI-SECTION METHOD Prof. M. P. Saka


ALGORITHM:
1. Choose lower xℓ and upper xu guesses for the root, so that the function
changes sign over the interval f(xℓ).f(xu) < 0.
x  xu
2. A estimate of the root xr is determined by xr  
2
3. Make following evaluation to determine which subinterval the root lies.
a) If f(xℓ).f(xr) < 0 , the root lies in the lower subinterval.
Therefore set xu = xr and return to 2.
b) If f(xℓ).f(xr) > 0 , the root lies in the upper subinterval.
Therefore set xℓ = xr and return to 2.
c) If f(xℓ).f(xr) = 0, the root equals to xr, terminate computation

MATLAB PROGRAM FOR BI-SECTION METHOD

%bisect.m program for bi-section method


xl=input('enter your guess for lower bound=');
xu=input('enter your guess for upper bound=');
es=input('enter relative error=');
imax=input('enter the maximum number of iterations=');
iter=0;
ea=1;
xr=xl;
disp([' iter xl xu xr fl fr
ea'])
while ea > es
xrold=xr;
xr=(xl+xu)/2;
iter=iter+1;
if iter > imax
disp(' maximum number of iterations is reached')
break
end
if xr ~=0
ea=abs((xr-xrold)/xr);
end
fl=parach(xl);
fr=parach(xr);
disp([iter xl xu xr fl fr ea])
test=fl*fr;
if test < 0
xu = xr;
elseif test > 0
14
xl = xr;
else
ea =0;
end
end
disp('number of iterations=')
disp(iter)
disp( 'root=')
disp(xr)

function y= parach(x)
y=667.38*(1-exp(-0.146843*x))/x-40;

EXAMPLE: Determine the root of by using


bi-section method. Take x  12 and xu  16 .

SOLUTION BY MATLAB PROGRAM

enter your guess for lower bound=12


enter your guess for upper bound=16
enter relative error=0.001
enter the maximum number of iterations=20

iter xl xu xr fl fr ea
1.0000 12.0000 16.0000 14.0000 6.0669 1.5687 0.1429
2.0000 14.0000 16.0000 15.0000 1.5687 -0.4248 0.0667
3.0000 14.0000 15.0000 14.5000 1.5687 0.5523 0.0345
4.0000 14.5000 15.0000 14.7500 0.5523 0.0590 0.0169
5.0000 14.7500 15.0000 14.8750 0.0590 -0.1841 0.0084
6.0000 14.7500 14.8750 14.8125 0.0590 -0.0629 0.0042
7.0000 14.7500 14.8125 14.7813 0.0590 -0.0020 0.0021
8.0000 14.7500 14.7813 14.7656 0.0590 0.0284 0.0011
9.0000 14.7656 14.7813 14.7734 0.0284 0.0132 0.0005

number of iterations= 9
root= 14.7734

15
REGULA FALSI METHOD

Regula falsi method is similar to


the bi-section method, in that
the initial interval (a,b) is
generated so as to bracket a root
of equation 𝑓(𝑥) = 0. Instead
of continuously bisecting the
interval, the linear interpolation
of 𝑓(𝑥) passing through the two
end points of the current
interval of uncertainty is used to
find a new approximation for
the root. Therefore, if 𝑥1 =
𝑎 𝑎𝑛𝑑 𝑥2 = 𝑏 denotes two
points on either side of the exact
root, a linear interpolation
(straight line) is assumed
through points [𝑥1 , 𝑓(𝑥1 )] and
[𝑥2 , 𝑓(𝑥2 )].

16
17
Example: Find the root of 𝑓(𝑥) = 4.905𝑥 2 − 15𝑥 + 5 = 0 within the interval 𝑥𝑙 = 0.3 𝑎𝑛𝑑 𝑥𝑢 =
0.4 with ε<5×10-4.

Example: Find a root of an equation f(x)=2x3-2x-5 using False Position method (regula falsi
method)

Solution:
2x3-2x-5=0 is the equation for which the root is to be found. Let f(x)=2x3-2x-5. Compute the values of
function for x=0, 1, 2

x 0 1 2
f(x) -5 -5 7

1st iteration : f(1)=-5<0 and f(2)=7>0 ∴ Now, Root lies between x0=1 and x1=2

( ) ( ) ( ),
x2=x0-f x0 ⋅x1-x0f x1 -f x0 x2=1-(-5)⋅2-17-(-5), x2=1.41667

( )
f x2 =f(1.41667)=2⋅1.416673-2⋅1.41667-5=-2.14699<0

18
2nd iteration : f(1.41667)=-2.14699<0 and f(2)=7>0, ∴ Now, Root lies between x0=1.41667 and x1=2

( ) ( ) ( ), x3=1.41667-(-2.14699)⋅2-1.416677-(-2.14699)=1.55359
x3=x0-f x0 ⋅x1-x0f x1 -f x0

( )
f x3 =f(1.55359)=2⋅1.553593-2⋅1.55359-5=-0.60759<0

3rd iteration : f(1.55359)=-0.60759<0 and f(2)=7>0,∴ Now, Root lies between x0=1.55359 and x1=2

( ) ( ) ( ), x4=1.55359-(-0.60759)⋅2-1.553597-(-0.60759)=1.58924
x4=x0-f x0 ⋅x1-x0f x1 -f x0

( )
f x4 =f(1.58924)=2⋅1.589243-2⋅1.58924-5=-0.15063<0

4th iteration : f(1.58924)=-0.15063<0 and f(2)=7>0, ∴ Now, Root lies between x0=1.58924 and x1=2

( ) ( ) ( ),x5=1.58924-(-0.15063)⋅2-1.589247-(-0.15063)=1.59789
x5=x0-f x0 ⋅x1-x0f x1 -f x0

( )
f x5 =f(1.59789)=2⋅1.597893-2⋅1.59789-5=-0.0361<0

5th iteration : f(1.59789)=-0.0361<0 and f(2)=7>0, ∴ Now, Root lies between x0=1.59789 and x1=2

( ) ( ) ( ), x6=1.59789-(-0.0361)⋅2-1.597897-(-0.0361)=1.59996
x6=x0-f x0 ⋅x1-x0f x1 -f x0

( )
f x6 =f(1.59996)=2⋅1.599963-2⋅1.59996-5=-0.00858<0

6th iteration : f(1.59996)=-0.00858<0 and f(2)=7>0, ∴ Now, Root lies between x0=1.59996 and x1=2

( ) ( ) ( ), x7=1.59996-(-0.00858)⋅2-1.599967-(-0.00858)=1.60045
x7=x0-f x0 ⋅x1-x0f x1 -f x0

( )
f x7 =f(1.60045)=2⋅1.600453-2⋅1.60045-5=-0.00203<0

7th iteration : f(1.60045)=-0.00203<0 and f(2)=7>0, ∴ Now, Root lies between x0=1.60045 and x1=2

( ) ( ) ( ), x8=1.60045-(-0.00203)⋅2-1.600457-(-0.00203)=1.60056
x8=x0-f x0 ⋅x1-x0f x1 -f x0

( )
f x8 =f(1.60056)=2⋅1.600563-2⋅1.60056-5=-0.00048<0

Approximate root of the equation 2x3-2x-5=0 using False Position method is 1.60056. Complete
calculations are given in the following table.

n x0 f(x0) x1 f(x1) x2 f(x2) Update


1 1 -5 2 7 1.41667 -2.14699 x0=x2
2 1.41667 -2.14699 2 7 1.55359 -0.60759 x0=x2
3 1.55359 -0.60759 2 7 1.58924 -0.15063 x0=x2
4 1.58924 -0.15063 2 7 1.59789 -0.0361 x0=x2
5 1.59789 -0.0361 2 7 1.59996 -0.00858 x0=x2
6 1.59996 -0.00858 2 7 1.60045 -0.00203 x0=x2
7 1.60045 -0.00203 2 7 1.60056 -0.00048 x0=x2

19
Open Methods

Fixed point iteration (successive substitution method):

20
Algorithm - Fixed Point Iteration Scheme

Given an equation f(x) = 0


Convert f(x) = 0 into the form x = g(x)
Let the initial guess be x0
Do
xi+1= g(xi)
while (none of the convergence criterion C1 or C2 is met)

 C1. Fixing a priori the total number of iterations n.


 C2. By testing the condition | xi+1 - g(xi) | (where i is the iteration number) less than
some tolerance limit, say epsilon, fixed a priori.

Example 1: Find a root of x4-x-10 = 0.

Let us re-write the equation as x4 -x=10, x(x3 -1)=10, x=10/(x3 -1).

In this case g1(x) = 10 / (x3-1) and the fixed point iterative scheme xi+1=10 / (xi3 -1), i = 0, 1, 2,..
let the initial guess x0 be 2.0

i 0 1 2 3 4 5 6 7 8
xi 2 1.429 5.214 0.071 -10.004 -9.978E-3 -10 -9.99E-3 -10

So the iterative process with g1 gone into an infinite loop without converging.

Another re-arrangement can be x4 =x+10, x = (x+10)1/4 , In this case g2(x) = (x + 10)1/4 and
the fixed point iterative scheme
xi+1= (xi + 10)1/4, i = 0, 1, 2, . . .

let the initial guess x0 be 1.0, 2.0 and 4.0

i 0 1 2 3 4 5 6
xi 1.0 1.82116 1.85424 1.85553 1.85558 1.85558
xi 2.0 1.861 1.8558 1.85559 1.85558 1.85558
xi 4.0 1.93434 1.85866 1.8557 1.85559 1.85558 1.85558

That is for g2 the iterative process is converging to 1.85558 with any initial guess.

Yet another arrangement can be x2×x2 -x-10=0, x2 =(x+10)/x2 , x=(x+10)1/2 /x

In this case g3(x) =(x+10)1/2/x and the fixed point iterative scheme
21
xi+1=( xi+10)1/2 /xi, i = 0, 1, 2, . . .

let the initial guess x0 be 1.8,

i 0 1 2 3 4 5 6 ... 98
xi 1.8 1.9084 1.80825 1.90035 1.81529 1,89355 1.82129 . . . 1.8555

That is for g3 with any initial guess the iterative process is converging but very slowly to

Geometric interpretation of convergence with g1, g2 and g3

Fig g1 Fig g2 Fig g3

The graphs in Fig g1, Fig g2 and Fig g3 demonstrates the Fixed point Iterative Scheme with g1,
g2 and g3 respectively for some initial approximations. It's clear from the

 Fig g1, the iterative process does not converge for any initial approximation.
 Fig g2, the iterative process converges very quickly to the root which is the intersection point
of y = x and y = g2(x) as shown in the figure.
 Fig g3, the iterative process converges but very slowly.

Example 2: Find the root of (cos[x]) -(x * exp[x]) = 0

Consider g(x) = cos[x]/exp[x], let the initial guess x0 be 2.0

i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 ... 31 32
xi 1 0.199 0.803 0.311 0.698 0.381 0.634 0.427 0.594 0.458 0.567 0.478 0.551 0.491 ... 0.518 0.518

That is for g(x) = cos[x]/exp[x] the iterative process is converged to 0.518.

22
Example 3: Find the root of x-exp[-x] = 0

Consider g(x) = exp[-x], let the initial guess x0 be 3.0

i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
xi 3.0 0.05 0.951 0.386 0.68 0.507 0.602 0.548 0.578 0.561 0.571 0.565 0.568 0.567 0.567

That is for g(x) = exp[-x] the iterative process is converged to 0.567.

Example 4: Find the root of (exp[-x] * (x2+5x+2)) + 1= 0

Consider g(x) = exp[x]+x^3+4x^2+2x+2)/(x^2+3x-3), let the initial guess x0 be -2

i 0 1 2 3 4 5 6
xi -2 -1.227 -0.776 -0.603 -0.58 -0.579 -0.679

That is for g(x) = (exp[x]+x^3+4x^2+2x+2)/(x^2+3x-3) the iterative process is converged to -


0.579.

Example 5:

The equation can be written as

1.5𝑥 −1
1 0.65𝑥
= 0.65𝑡𝑎𝑛 ( ) −
(1 + 𝑥 2 )2 𝑥 (1 + 𝑥 2 )

13 1 13
Which in turn can be written as 𝑥 = (1 + 𝑥 2 )2 𝑡𝑎𝑛−1 ( ) − 𝑥(1 + 𝑥 2 )
30 𝑥 30

Then from 𝑥 = 𝑔(𝑥), g(x) will have the following expression

23
FIXED POINT ITERATION METHOD Prof. M. P. Saka

ALGORITHM:
1. Choose x1 as initial guess for the root.
2. Calculate the new root in each iteration i as x i 1  g ( x i ) ,
3. Terminate calculations when  a   s .

MATLAB PROGRAM FOR FIXED-POINT ITERATIONS METHOD

%matlab program for fixed point iterations method


xn=input('enter the initial value of x =');
es=input('enter the preselected relative error=');
imax=input('enter the maximum number of iterations=');
iter=0;
ea=1;
disp([' iter xn ea'])
while ea > es || iter > imax
x0=xn;

24
xn=g(x0);
iter=iter+1;
if xn ~=0
ea=abs((xn-x0)/xn);
end
disp([iter xn ea])
end
disp('number of iterations=')
disp(iter)
disp('root is equal to')
disp(xn)

function y= g(x)
y=exp(-x)

Program is run to determine the root of equation 𝑥 − 𝑒 −𝑥 = 0. The form selected for the method
is 𝑥 = 𝑒 −𝑥 which yields 𝑔(𝑥) = 𝑒 −𝑥 .

enter the initial value of x =1


enter the preselected relative error=0.001
enter the maximum number of iterations=50
iter xn ea
1.0000 0.3679 1.7183
2.0000 0.6922 0.4685
3.0000 0.5005 0.3831
4.0000 0.6062 0.1745
5.0000 0.5454 0.1116
6.0000 0.5796 0.0590
7.0000 0.5601 0.0348
8.0000 0.5711 0.0193
9.0000 0.5649 0.0111
10.0000 0.5684 0.0062
11.0000 0.5664 0.0036
12.0000 0.5676 0.0020
13.0000 0.5669 0.0011
14.0000 0.5673 0.0006
number of iterations= 14 root is equal to 0.5673

25
Newton Raphson method

The Newton-Raphson method is well-known and most powerful method used for finding the root
of the equation f(x)=0. It can be derived by using Taylor’s series expansion of the function f(x)
about an arbitrary point x0 as

26
27
Example 1:

Example 2:

28
Newton -Raphson method may not converge to a solution as shown in the following figure.
These difficulties can be corrected by starting from a new initial guess point.

29
30
MATLAB Program for Newton-Raphson Method

%raphson.m program for newton method


xo=input('enter your guess for the root=');
es=input('enter relative error=');
imax=input('enter the maximum number of iterations=');
iter=0;
ea=1;
xn=xo;
disp(' iter xo f fd xn
ea')
while ea > es
xo=xn;
iter=iter+1;
f=(2/xn-xn)*cos(xn)+0.0119;
fd=(-2/xn^2-1)*cos(xn)-(2/xn-xn)*sin(xn);
xn = xo - f/fd;
if xn ~=0
ea=abs((xn-xo)/xn);
end

disp([iter,xo,f,fd,xn,ea])
if iter > imax
disp(' maximum number of iterations is reached')
break
end

end
disp('number of iterations=')
disp(iter)
disp( 'root=')
disp(xn)

OUTPUT:
enter your guess for the root=1
enter relative error=0.001
enter the maximum number of iterations=25
iter xo f fd xn ea
1.0000 1.0000 0.5522 -2.4624 1.2243 0.1832
2.0000 1.2243 0.1509 -1.1779 1.3524 0.0948
3.0000 1.3524 0.0393 -0.5770 1.4205 0.0479
4.0000 1.4205 0.0100 -0.2857 1.4556 0.0241
5.0000 1.4556 0.0025 -0.1425 1.4733 0.0120
6.0000 1.4733 0.0006 -0.0718 1.4820 0.0059
7.0000 1.4820 0.0002 -0.0373 1.4861 0.0027
8.0000 1.4861 0.0000 -0.0215 1.4876 0.0010
9.0000 1.4876 0.0000 -0.0156 1.4879 0.0002
number of iterations= 9
root= 1.4879
31
Secant method

32
33
34
COMPARISON OF CONVERGENCE OF THE SECANT AND FALSE POSITION
METHOD

Use the false-position and secant methods to estimate the root off ( x)  ln( x) . Start the
computation with values of xl  xi 1  0.5 and xu  xi  5.0 .

Using the false position method the following result is obtained.

enter your guess for lower bound=0.5


enter your guess for upper bound=5
enter relative error=0.001
enter the maximum number of iterations=10

iter xl xu xr f(xl) f(xu) f(xr)


1.0000 0.5000 5.0000 1.8546 -0.6931 1.6094 0.6177
2.0000 0.5000 1.8546 1.2163 -0.6931 0.6177 0.1958
3.0000 0.5000 1.2163 1.0585 -0.6931 0.1958 0.0569
4.0000 0.5000 1.0585 1.0162 -0.6931 0.0569 0.0160
5.0000 0.5000 1.0162 1.0045 -0.6931 0.0160 0.0045
6.0000 0.5000 1.0045 1.0013 -0.6931 0.0045 0.0013
7.0000 0.5000 1.0013 1.0003 -0.6931 0.0013 0.0003

number of iterations= 7

root= 1.0003

Using the secant method the following results are obtained.

enter your first guess for the root=0.5


enter your second guess for the root=5
enter relative error=0.001
enter the maximum number of iterations=15
iter xi-1 xi xi+1 ea f(xi-1) f(xi)
1.0000 0.5000 5.0000 1.8546 1.6959 -0.6931 1.6094
2.0000 5.0000 1.8546 -0.1044 18.7680 1.6094 0.6177

Secant method is diverging.

35
COMPARISON OF DIFFERENT METHODS FOR SOLVING A NONLINEAR
EQUATION
x
Let us estimate the root of f ( x)  e  x using Newton-Raphson, Bi-section, False position,
Secant and fixed point-iterations methods with an initial guess of xo= 0.

1) Newton-Raphson Method

enter your guess for the root=0


enter relative error=0.001
enter the maximum number of iterations=10
iter xo xn ea f fd
1.0000 0 0.5000 1.0000 1.0000 -2.0000
2.0000 0.5000 0.5663 0.1171 0.1065 -1.6065
3.0000 0.5663 0.5671 0.0015 0.0013 -1.5676
4.0000 0.5671 0.5671 0.0000 0.0000 -1.5671

number of iterations= 4 root= 0.5671

2) Bi-section method

enter your guess for lower bound=-1


enter your guess for upper bound=1
enter relative error=0.001
enter the maximum number of iterations=20
iter xl xu xr f (x l) f (x r)
1.0000 -1.0000 1.0000 0 3.7183 1.0000
2.0000 0 1.0000 0.5000 1.0000 0.1065
3.0000 0.5000 1.0000 0.7500 0.1065 -0.2776
4.0000 0.5000 0.7500 0.6250 0.1065 -0.0897
5.0000 0.5000 0.6250 0.5625 0.1065 0.0073
6.0000 0.5625 0.6250 0.5938 0.0073 -0.0415
7.0000 0.5625 0.5938 0.5781 0.0073 -0.0172
8.0000 0.5625 0.5781 0.5703 0.0073 -0.0050
9.0000 0.5625 0.5703 0.5664 0.0073 0.0012
10.0000 0.5664 0.5703 0.5684 0.0012 -0.0019
11.0000 0.5664 0.5684 0.5674 0.0012 -0.0004
12.0000 0.5664 0.5674 0.5669 0.0012 0.0004

number of iterations= 12 root=0.5669

3) False position Method

enter your guess for lower bound=-1


enter your guess for upper bound=1
enter relative error=0.001
enter the maximum number of iterations=20

36
iter xl xu xr f (x l) f (x u) f (x r)
1.0000 -1.0000 1.0000 0.7094 3.7183 -0.6321 -0.2175
2.0000 -1.0000 0.7094 0.6149 3.7183 -0.2175 -0.0743
3.0000 -1.0000 0.6149 0.5833 3.7183 -0.0743 -0.0253
4.0000 -1.0000 0.5833 0.5726 3.7183 -0.0253 -0.0086
5.0000 -1.0000 0.5726 0.5690 3.7183 -0.0086 -0.0029
6.0000 -1.0000 0.5690 0.5678 3.7183 -0.0029 -0.0010
7.0000 -1.0000 0.5678 0.5674 3.7183 -0.0010 -0.0003

number of iterations= 7 root= 0.5674

4) Secant Method

enter your first guess for the root=-1


enter your second guess for the root=1
enter relative error=0.001
enter the maximum number of iterations=20
iter x i-1 xi x i+1 ea f (x i-1) f (x i)
1.0000 -1.0000 1.0000 0.7094 0.4096 3.7183 -0.6321
2.0000 1.0000 0.7094 0.5570 0.2736 -0.6321 -0.2175
3.0000 0.7094 0.5570 0.5674 0.0183 -0.2175 0.0159
4.0000 0.5570 0.5674 0.5671 0.0005 0.0159 -0.0004

number of iterations= 4 root= 0.5671

5) Fixed point iterations

enter the initial value of x =0


enter the preselected relative error=0.001
enter the maximum number of iterations=20
iter xl ea
1 1 1
2.0000 0.3679 0.6321
3.0000 0.6922 0.3243
4.0000 0.5005 0.1917
5.0000 0.6062 0.1058
6.0000 0.5454 0.0608
7.0000 0.5796 0.0342
8.0000 0.5601 0.0195
9.0000 0.5711 0.0110
10.0000 0.5649 0.0063
11.0000 0.5684 0.0035
12.0000 0.5664 0.0020
13.0000 0.5676 0.0011
14.0000 0.5669 0.0006
number of iterations= 14 root = 0.5669
37
fzero function in MATLAB

MATLAB function named fzero finds zero of a function of one variable.

z=fzero(‘function’,xo) finds a zero of the function f(x) that is near to xo. fzero identifies only
points where the function actually crosses the x-axis. Points where the function touches the x-axis,
but does not cross it are not considered zeros.

z=fzero(‘function’,xo,tol) returns an answer accurate to within a relative error of tol.

z=fzero(‘function’,xo,tol,trace) displays information at each iteration if trace is nonzero.

EXAMPLE 1: To find the zero of the function f(x) = x 3 – 2 x – 5 , first we have to write an M-
file as shown in the following and save it as f.m in a directory of your selection.

function y=f(x)
y=x.^3-2*x-5

Then by just typing the following in the command window the root will appear on the screen in
no time.
z = fzero(‘f’, 2)
z=
2.0946
EXAMPLE 2: Determine the root of f ( x) 
667.38
x
 
1  e  0.146843x  40 .
Prepare Matlab function and save it as parachuter.m file.

function y=parachuter(x)
f=667.8*(1-exp(-0.146843*x))./x-40

Then type the following in the command window to determine the root of the nonlinear equation.

root = fzero('parachuter',10)

or you can directly insert the function in fzero tool.

root =fzero(‘667.8*(1-exp(-0.146843*x))./x-40’, 10)

press “enter” and you will see the following output on the command window.

root =

14.7802

38

You might also like