Lecture Notes 2

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 76

CHAPTER 2

Finding the Roots of


Equations

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Introduction

• Years ago, you learned to use the quadratic formula


b  
x1,2  where   b 2  4ac (1)
2a
• to find the roots of
f ( x)  ax 2  bx  c  0 (2)

• The values calculated with Eq. (1) are called the


“roots” of Eq. (2).

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Introduction

• They represent the values of x that make Eq. (2)


equal to zero.
• Thus, we can define the root of an equation as the
value of x that makes f (x) = 0.
• For this reason, roots are sometimes called the
“zeros’’ of the equation.
• Although the quadratic formula is handy for
solving Eq. (2), there are many other functions for
which the root cannot be determined so easily.

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Introduction

• Linear: Independent variable appears to the first


power only, either alone or multiplied by a
constant
Example: f(x)=2x-3…………..root=3/2
• Nonlinear:
– Polynomial: Independent variable appears
raised to powers of positive integers only
Example: f(x)=x2-3x+2……..roots=1 and 2
– General non-linear: all other equations
Example: f ( x)  e x  sin( x) How to find the root?
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Introduction

• For these cases, the numerical methods must be


used to obtain the answer.

• Generally two kind of methods are used to obtain


the roots of functions:

1. Bracketing Methods
2. Open Methods

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Introduction

• Bracketing methods: As the name implies, these


are based on two initial guesses that bracket the
root. Those are on either side of the root.

• Open methods: These methods can involve one or


more initial guesses, but there is no need for them
to bracket the root.

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Introduction

• For well-posed problems, the bracketing methods


always work but converge slowly.

• In contrast, the open methods do not always work


(i.e., they can diverge), but when they do they
usually converge quicker.

• In both cases, initial guesses are required. These


may naturally arise from the physical problem you
are solving.
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
2.1. Bracketing Methods
• The functions change signs in the existence of root.

f(xu) f(x)

xl root
xu

f(xl)

• As shown in Figure, f(xl) is negative but f(xu) positive


value. Therefore the function f(x) is a root between xl
and xu
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
2.1.1 Graphical Methods

• A simple method for obtaining an estimate of the


root of the equation f (x) = 0 is to make a plot of
the function and observe where it crosses the x
axis.

• This point, which represents the x value for which


f (x) = 0, provides a rough approximation of the
root.

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


2.1.1 Graphical Methods
• Example 2.1
• The equation for free-falling body near the earth
surface are given as follows:

mg    t 
c

v(t )  1  e  m  
c  

• Use the graphical approach to determine the mass
of the bungee jumper with a drag coefficient of
c=4 kg/m to have a velocity of v=36 m/s after t=4 s
of free fall. Note: The acceleration of gravity is
g=9.81 m/s2.
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
2.1.1 Graphical Methods
• If the given function equalize to zero:

mg    t 
c
m
f ( m)   1  e v  0
c  

• The following MATLAB code sets up a plot of f(m) versus
mass

• c = 4; g = 9.81; v = 36; t = 4;
• m = 50:150;
• f = (m*g/c).*(1-exp(-c*t./m))-v;
• plot(m,f),grid

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


2.1.1 Graphical Methods
• The function crosses the m axis between 90 and 95
kg
1.5

0.5

0
f(m)

-0.5

-1 Root

-1.5

-2

-2.5
50 60 70 80 90 100 110 120 130 140 150
m
• The function crosses the m axis between 90 and 95
kg
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
2.1.2 Bisection Method

• Bracketing methods. As the name implies, these are based


on two initial guesses that bracket the root. Those are on
either side of the root.
• In Example 2.1, you observed that f (x) changed sign on
opposite sides of the root. In general, if f (x) is real and
continuous in the interval from xl to xu and f (xl ) and f (xu)
have opposite signs, that is,
f (xl )* f (xu) < 0
• Then there is at least one real root between xl and xu .

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


2.1.2 Bisection Method
• As observed from Figure, the function is negative at xl=80
but positive at xu=120. That is f (xl ) and f (xu) have opposite
signs. 1.5

0.5 mg 
f ( m)  1
0
c 
f(m)

-0.5

-1

Root
-1.5

-2

-2.5
50 60 70 80 90 100 110 120 130 140 150

xl m
xu

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


2.1.2 Bisection Method
• Then the interval between xl and xu is divided two
subinterval as shown. xr is calculated as:
1.5

xl  xu 1

xr  0.5

2 0

f(m )
-0.5

• xr is first estimation -1

for the root.


-1.5

-2

-2.5
50 60 70 80 90 100 110 120 130 140 150

xl m
xu

xr
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
2.1.2 Bisection Method
• Now the root is either between xl and xr (subinterval
1 ) or between xr and xu (subinterval 2).
1.5

0.5

0
f(m )

-0.5

-1

-1.5

-2

-2.5
50 60 70 80 90 100 110 120 130 140 150

xl m
xu

xr
Subinterval 1 Subinterval 2

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


2.1.2 Bisection Method
• If f (xl )* f (xr) < 0, the root in subinterval 1 (between xl and xr). Then xr is
the new xu value.

• Otherwise (If f (xl )* f (xr) > 0) the root in subinterval 2 (between xr and
xu). Then xr is the new xl value.

• New xr value is calculated using new xl and xu . And contunie same


procedure until we reach

a  s
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
2.1.2 Bisection Method

• where;

new old
xr  xr
a  new
xr

 s  (0.5 102 n )% n is significant figures.

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


2.1.2 Bisection Method
• ALGORITHM

• Step1: Choose lower xl and upper xu guesses for


the root, so that the function changes sign over the
interval f (xl )* f (xu) < 0.

• Step2: An estimate of the root xr is determined by:

xl  xu
xr 
2
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
2.1.2 Bisection Method
• Step3: Make the following evaluation to determine in
which subinterval the root lies.
i. If f (xl )* f (xr) < 0, the root lies in the subinterval
1(between [xl ,xr]). Therefore set xu=xr and return to
Step 2.

i. If f (xl )* f (xr) > 0, the root lies in the upper


subinterval 2 ( between [xr , xu]). Therefore set xl=xr
and return to Step 2.

ii. Contunie the iterations until a  s


CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Example

• The function f ( x)  x3  4 x 2  10 has a root between


[1,2] since f(1)=-5 and f(2)=14. Find the root using
bisection. εs=0.03

• Solution:
xl  1 xu  2 f (1)  5 f (2)  14

xl  xu 1  2
xr    1.5 f ( xr )  f (1.5)  2.375
2 2

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Solution
 Check

f ( xl )  f ( xr )  (5)  (2.375)  11.875  0

 Then new xu=1.5 and xl=1 (root is between 1 and


1.5)
xl  xu 1  1.5
xr    1.25 f ( xr )  f (1.25)  1.80
2 2

 Approximate Error
xrnew  xrold 1.25  1.5
a  new
  0.2   s
xr 1.25
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Solution
 Check

f ( xl )  f ( xr )  ( 5)  (1.8)  9  0

 Then new xl=1.25 and xu=1.5 (root is between 1.25


and 1.5)
xl  xu 1.25  1.5
xr    1.375 f ( xr )  f (1.375)  0.16
2 2

 Approximate Error
xrnew  xrold 1.375  1.25
a  new
  0.09   s
xr 1.375
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Solution
• New iteration
f ( xr )  f (1.375)  0.16

f ( xl )  f ( xr )  (1.80)  (0.16)  0.288  0

• Then new xl=1.25 and xu=1.375 (root is between


1.25 and 1.375)
1.25  1.375
xr   1.3125
2
xrnew  xrold 1.3125  1.375
a  new
  0.048   s
xr 1.3125
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Solution
• New iteration
f ( xr )  f (1.3125)  0.84

f ( xl )  f ( xr )  (1.80)  (0.84)  1.5  0

• Then new xl=1.3125 and xu=1.375 (root is between


1.3125 and 1.375)
1.3125  1.375
xr   1.34375
2
xrnew  xrold 1.34375  1.3125
a  new
  0.024   s Root=1.34375
xr 1.34375
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Example

• The equation for free-falling body near the earth


surface are given as follows:
mg    t 
c

v (t )  1  e  m  
c  

• Calculate drag coefficient c for given values,


m=68.1kg, v=40 m/s, t=10sec and g=9.81 m/s2
using bisection method. (cl=12, cu=16 and
εs=0.5%)

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Example
• Subtituting the given values in the equation, we can get

68.1 9.81  
 c 

68.1
10 
40  1  e   
c  
 
• Then rearranging the equation

667.38
c
1  e 0.1468 c
  40  0

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


• Step 1

f (12)  6.047 f (16)  2.27


12  16 f (14)  1.57
xr   14
2
• Check f (12)  f (14)  6.047 1.57  9.5  0
• The root between 14 and 16
• Step 2

14  16 f (15)  0.425
xr   15
2
• Check f (14)  f (15)  1.565  (0.425)  0.665  0
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
15  14
a  100  6.667%
15
• The root between 14 and 15
• Step 3
14  15
xr   14.5
2
• Check f (14)  f (14.5)  (1.565)  (0.5489)  0.8591  0

14.5  15
a   100  3.45%
14.5
• The root between 14.5 and 15
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Iteration xl xu xr εa (%)
1 12 16 14 (14-12)/
14
2 14 16 15 6.67
3 14 15 14.5 3.45
4 14.5 15 14.75 1.70
5 14.75 15 14.875 0.84
6 14.75 14.875 14.8125 0.42

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Matlab Code for Bisection Method
xl=input('enter your guess for lower bound=');
xu=input('enter your guess for upper bound=');
es=input('enter relative error=');
fxl = f(xl);
fxu = f(xu);
ea=100;
iter=0;
xr=xl
disp([' no of iteration root error'])
while ea > es
xro=xr

xr = (xl +xu)/2;
fxr = f(xr);
if fxl * fxr < 0
xu = xr;
fxu = fxr;
else
xl = xr; function [ y] = f( x )
fxl = fxr; y=(667.38/x)*(1-exp(-0.1468*x))-40;
end end
ea=abs((xr-xro)/xr);
iter=iter+1;
disp([iter,ea,xr])
end
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Solution with Matlab
• enter your guess for lower bound=12
• enter your guess for upper bound=16
• enter relative error=0.005
• no of iteration root error
• 1.0000 0.1429 14.0000

• 2.0000 0.0667 15.0000

• 3.0000 0.0345 14.5000

• 4.0000 0.0169 14.7500

• 5.0000 0.0084 14.8750

• 6.0000 0.0042 14.8125

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


False Position Method
• False position (also called the linear interpolation method)
is another well-known bracketing method.
• It is very similar to bisection but it uses a different strategy
for new root estimate. it locates the root by joining f (xl )
and f (xu) with a straight line (Figure).

f ( xl ) f ( xu )

xr  xl xr  xu

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


False Position Method
• The intersection of this line with the x axis represents an
improved estimate of the root.

• Thus, the shape of the function influences the new root


estimate. Using similar triangles, the intersection of the
straight line with the x axis can be estimated as

f ( xu )( xl  xu )
xr  xu 
f ( xl )  f ( xu )

• Only the difference between bisection and false position


method is to calculate xr
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Example

• Use the false-position method to determine the


root of the same equation investigated previous
Free falling example
667.38
x
1  e 0.1468 x
  40  0

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Solution
• First iteration

xl  12 f ( xl )  6.067
xu  16 f ( xu )  2.269
f ( xu )( xl  xu ) 2.269(12  16)
xr  xu   16   14.91
f ( xl )  f ( xu ) 6.067  (2.269)
• Second iteration

f ( xl )  f ( xr )  f (12)  f (14.91)  (6.067)  ( 0.254)

 1.543
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Solution
• Therefore the root between xl and xr

xl  12 f ( xl )  6.067
xu  14.91 f ( xu )  0.254

0.254(12  14.91)
xr  14.91   14.794
6.067  (0.254)

14.794  14.91
a   0.0079
14.794

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Solution with Matlab
• xl=input('enter your guess for lower bound=');
• xu=input('enter your guess for upper bound=');
• es=input('enter relative error=');
• fxl = f(xl);
• fxu = f(xu);
• ea=100;
• iter=0;
• xr=xl;
• disp([' no of iteration root error'])
• while ea > es
• xro=xr;

• xr = xu-fxu*(xl-xu)/(fxl-fxu); Only calculation of xr is d


• fxr = f(xr); from Bisection method
• if fxl * fxr < 0
• xu = xr;
• fxu = fxr;
• else function [ y] = f( x )
• xl = xr; y=(667.38/x)*(1-exp(-0.1468*x))-40;
• fxl = fxr; end
• end
• ea=abs((xr-xro)/xr);
• iter=iter+1;
• disp([iter,xr,ea])
• end CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Solution with Matlab

• enter your guess for lower bound=12


• enter your guess for upper bound=16
• enter relative error=0.005
• no of iteration root error
• 1.0000 14.9097 0.1952

• 2.0000 14.7925 0.0079

• 3.0000 14.7800 0.0008

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


OPEN METHODS
• Fixed (One) Point Iterations

• Open methods employ a formula to predict the root. Such a


formula can be developed for simple fixed (one) point
iteration by rearranging the function f (x) = 0 so that x is on
the left-hand side of the equation:

x  g ( x)

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


• Examples:

x2  2 x  3  0
x2  3
• Can be simply manupilated to yeild as: x
2

• whereas sin x = 0 could be put into the fixed point


form by adding x to both sides to yield
x  x  sin( x)

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Fixed (One) Point Iterations

• The utility of x=g(x) is that it provides a formula to predict a


new value of x as a function of an old value of x. Thus, given an
initial guess at the root xi, x=g(x) can be used to compute a
new estimate xi+1 as expressed by the iterative formula:
xi 1  g ( xi )

• As with other iterative formulas in this book, the approximate


error for this equation can be determined using the error
estimator
xi 1  xi
a   s
xi 1
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Example

• Use simple fixed-point iteration to locate the root


of f (x) = e−x − x.

• The function can be separated directly and expressed in


the form of Equation as:

xi+1 = e−xi

• Starting with an initial guess of x0 = 0, this iterative


equation can be applied to compute
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Solution
i  0  x1  e 0  1
i  1  x2  e  x1  0.367879
i  2  x3  e  x2  0.692201
i  3  x4  e  x3  0.500472

The value is converging to the


root=0.56714329

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Matlab code

• xn=0;
• es=1;
• ea=2*es;
• i=0;
• disp([' no root ea'])
• while es<ea
• x0=xn;
• xn=f(x0);
• ea=abs(((xn-x0)/xn)*100);
• i=i+1;
function [ y ] = f( x );
• disp([i xn ea])
y=exp(-x);
• end end

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Solution with Matlab

• No root Error
• 1 1 100
• 2.0000 0.3679 171.8282
• 3.0000 0.6922 46.8536
• 4.0000 0.5005 38.3091
• 5.0000 0.6062 17.4468
• 6.0000 0.5454 11.1566
• 7.0000 0.5796 5.9034
• 8.0000 0.5601 3.4809
• 9.0000 0.5711 1.9308
• 10.0000 0.5649 1.1089

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Example

• Consider a simple example


f ( x)  x 2  2 x  3
• This function has roots of x1=3 and x2=-1

• Rearranging the function to a form


x  2x  3 so g ( x)  2 x  3

Starting with x1=4 performing the iteration


xi 1  2 xi  3
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Solution

• This iteration converge to x=3 as follows


i  1  x2  2  4  3  3.316 xi 1  2 xi  3
i  2  x3  2  3.316  3  3.104
i  3  x4  3.011 x1=4
i  4  x5  3.004
.
. Home Exercise:
i  x3 Perform these
iterations using Matlab
code!!!
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Solution
• If we arrange the function as follows:
3
x( x  2)  3  x
x2

• Starting with x1=4 performing the iteration

3
xi 1 
xi  2

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Solution

• This iteration converge to x=-1 as follows


3 3
i 1  x2   1.5 xi 1 
x1  2 xi  2
3
i2  x3   6
x2  2
3
i3  x4   0.375
x3  2
i4  x5  1.263
Home Exercise:
.
Perform these
.
iterations using Matlab
i  x  1
code!!!
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Solution

• If we arrange the function as follows:


x2  3
2x  x2  3  x
2
• Starting with x1=4 performing the iteration

xi 2  3
xi 1 
2

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Solution

• This iteration will not converge to any value


(diverging) x 2 3
xi 1  i
x12  3 42  3 2
i  1  x2    6.5
2 2
x2 2  3
i  2  x3   19.6
2
x32  3
i  3  x4   191
2
. Home Exercise:
. Perform these iterations
i  x using Matlab code!!!
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
• Note that: Convergence only occurs when the
absolute value of the slope y2=g(x) is less than the
slope of y1=x, that is when g ( x)  1

a) g ( x)  2 x  3 g ( x)  (2 x  3) 1/2

g (4)  (2  4  3) 1/ 2  0.3015  1


Substituting x=4
Converging

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


b) 3 3
g ( x)  g ( x)  
x2 ( x  2) 2

Substituting x=4
3
g (4)  2  0.75  1
2 Converging
c)
x2  3
g ( x)x=4
Substituting  g ( x)  x
2

g (4)  4  4  1
Diverging
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Newton Raphson Method
• Perhaps the most widely used of all root-locating formulas
is the Newton-Raphson equation (Figure). If the initial
guess at the root is xi, a tangent can be extended from the
point [xi, f (xi)]. The point where this tangent crosses the x
axis usually represents an improved estimate of the root.

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Newton Raphson Method

• The Newton-Raphson method can be derived on


the basis of this geometrical interpretation. As in
Figure the first derivative at x is equivalent to the
slope:

• Which can be rearranged to yeild:

Newton Raphson
formula
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Example

• Use the Newton-Raphson method to estimate the


root of
f ( x)  e  x  x
employing an initial guess of x1=0
Solution
• The first derivative of the function is:
f ( x)  e  x  1
• Which can be substituted in the Newton-Raphson
formula to get:
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Solution

•Starting with an initial guess of x1=0, this iterative


equation can be applied to compute

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Matlab Code

xn=0;
es=0.001;
ea=10;
i=1;
while es<ea function [ y ] = fd( x )
x0=xn; y=-exp(-x)-1;
end
xn=x0-f(x0)/fd(x0);
ea=abs((xn-x0)/xn);
disp([ i, xn, ea]) function [ y ] = f( x )
i=i+1; y=exp(-x)-x;
end end

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Solution with Matlab

• i root error
• 1.0000 0.5000 1.0000

• 2.0000 0.5663 0.1171

• 3.0000 0.5671 0.0015

• 4.0000 0.5671 0.0000

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Example

• Calculate the root of following function using


Newton Raphson method. (x1=1 and εs=0.001)
f ( x)  xe x  1

• Solution
Newton Raphson Formula

Derivative of Function f ( x)  e x  xe x  ( x  1)e x

Iterative formula for


xi e xi  1
xi 1  xi 
solution ( xi  1)e xi
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Solution

• The solution for each step is given in following Table

i xi f(xi) f’(xi) xi+1 εa


1 1 1.7188 5.4366 0.6839 0.4621
2 0.6839 0.3553 3.3370 0.5775 0.1844
3 0.5775 0.0287 2.8102 0.5672 0.018
4 0.5672 0.00024 2.7636 0.5671 0.00017

• Note that in the last step εa< εs (0.00017<0.001) Then


iteration is terminated. Home Exercise:
The root=0.5671 Perform these iterations
using Matlab code!!!
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Example (Test 1 , 2014-15)

The following equation can be used to compute the oxygen level c (mg/L)
in a river downstream from a sewage discharge:

c = 10 − 20(e−0.15x − e−0.5x )

Determine the distance downstream (x) where the oxygen level is c=5
mg/L. Use Newton Raphson Method, Develop a while loop to determine
your answer. (εs=0.01 x0=2) . Each step of iteration, calculate the root (xi)
and approximate error and write the given Table below. Prove your
answer plotting the function between 0<x<2.

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Solution
xn=2; 5

es=0.001; 4

ea=10 3
i=0
2
disp([' no root ea'])
while es<ea 1

x0=xn; 0

xn=x0-f(x0)/fd(x0); -1

ea=abs(((xn-x0)/xn)); -2

i=i+1; -3
disp([ i xn ea]) 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

end
Number
function [ y ] = f( x )    
of
y=5-20*(exp(-0.15*x)-exp(-0.5*x)); iteration xi εa
end 1 0.3117 5.4170
  2 0.8434 0.6304
function [ y ] = fd( x ) 3 0.9699 0.1304
y=-20*(-0.15*exp(-0.15*x)+0.5*exp(-0.5*x)); 4 0.9762 0.0065
end CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Secant Method
• A potential problem in the Newton-Raphson method is the
evaluation of the derivative. Because, there are certain
functions whose derivatives may be extremely difficult or
inconvenient to evaluate. For these cases, the derivative
can be approximated by a backward finite divided
difference, as:

f ( xi )  f ( xi 1 )
f ( xi ) 
xi  xi 1

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Secant Method

• Substituting this derivative in the Newton Raphson


formula, following iterative formula is obtained as follows:

Secant Method
Formula

• Notice that the Secant Method Formula requires two initial


estimates of x ( xi and xi-1 requires to get xi+1 ).

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Example

• Determine the root of following function using secant


method (εs=0.01 , xi-1=0.5 and xi=0.4)

f ( x)  8sin( x)e  x  1

• Solution
Secant Method
Formula

• i=2 x1=0.5 and x2=0.4 then we can calculate x3 as


follows: f ( x2 )( x1  x2 )
x3  x2 
f ( x1 )  f ( x2 )
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Solution

• The solution for each step is given in following Table

i xi-1 xi f(xi-1) f(xi) xi+1 εa


1 0.5 0.4 1.3263 1.0883 -0.0572 NA
2 0.4 -0.0572 1.0883 -1.4846 0.2066 7.99
3 -0.0572 0.2066 -1.4846 0.3347 0.1581 1.28
4 0.2066 0.1581 0.3347 0.0751 0.1440 0.31
5 0.1581 0.1440 0.0751 -0.0058 0.1450 0.98
6 0.1440 0.1450 -0.0058 0.0001 0.1450 0.007

• Note that in the last step εa< εs (0.007<0.01) Then iteration is


terminated.
The root=0.1450
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Matlab Code
x(1)=0.5;
x(2)=0.4;
es=0.01;
ea=10;
i=2;
disp([' i root gx error'])
while es<ea
a=g(x(i))*(x(i)-x(i-1));
b=g(x(i))-g(x(i-1));
x(i+1)=x(i)-a/b;
ea=abs((x(i+1)-x(i))/x(i+1));
i=i+1;
function [y ] = g( x )
disp([i x(i) ea]) y=8*sin(x)*exp(-x)-1;
end end
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Example

The tension T in the cable can be represented by


the following formula as a function of x.

T  WLc Lb / Lb 2 x 2  x 4

where Lb=6 m, Lc=4.5 m and W=50 kN


Calculate the distance x for minimum tension (T) in the cable using
Secant Method. (Hint: dT/dx=0)
a) Develop a Matlab code for solution of x (x1=1 x2=2 εs=0.001).
b) Calculate the Minimun Tension in Cable.
c) Plot the T versus x between 1<x<5 and verify your solution.

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Solution

• Derivative of the function (dT/dx):

3
dT 1 
  (WLc Lb )( Lb 2 x 2  x 4 ) 2 (2 Lb 2 x  4 x 3 )  0
dx 2

• Then the root of this derivative should be calculated

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Solution
 x(1)=1; function [y ] = f( x )
x(2)=2; w=50;
es=0.001; lb=6;
lc=4.5;
ea=10; y=-0.5*(w*lb*lc)*(lb^2*x^2-x^4)^
i=2; (-3/2)*(2*lb^2*x-4*x^3);
while ea>es end
a=f(x(i))*(x(i)-x(i-1));
b=f(x(i))-f(x(i-1));
x 4.2426 m
x(i+1)=x(i)-a/b;
Tmin 75 kN
ea=abs((x(i+1)-x(i))/x(i+1));
disp([x(i+1),ea]) Note that: Tmin is calculate
i=i+1; substituting x value in
end the function of

T  WLc Lb / Lb 2 x 2  x 4
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Solution

240

220

200

180

160
T (kN)
140

120

100

80 Minimum value
of Tension
60
1 1.5 2 2.5 3 3.5 4 4.5 5
x (m)

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Modified Secant Method

• Rather than using two arbitrary values to estimate the


derivative, a fractional perturbation (δ) is used. Then
derivative of the function is estimated as follows:

f ( xi   xi )  f ( xi )
f ( xi ) 
 xi

• If this approximation is substituted in Newton Raphson


Formula, Modified Secant Method Formula is obtained as
follows:

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Example

CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN


Matlab Code
d=0.01;
x(1)=1;
ea=10; Outputs
es=0.001; i root error
i=1; 2.0000 0.5373 0.8613
disp([' i root error'])
3.0000 0.5670 0.0525
while ea>es
fx=f(x(i)); 4.0000 0.5671 0.0002
fdx=f(x(i)+d*x(i));
x(i+1)=x(i)-(d*x(i)*fx)/(fdx-fx);
ea=abs((x(i+1)-x(i))/x(i+1));
disp([i x(i) ea])
i=i+1;
end
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN

You might also like