0% found this document useful (0 votes)
37 views24 pages

Numerical Methods: Dr. Nasir M Mirza

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 24

Roots of Nonlinear Equations

Topic: Secant Method


Dr. Nasir M Mirza
Numerical Methods
Email: nasirmm@yahoo.com
Secant Method
) (x f
) f(x
- = x x
i
i
i i
'
+1

f(x)
f(x
i
)
f(x
i-1
)
x
i+2
x
i+1
x
i

X
u

( ) | |
i i
x f x
,


1
1
) ( ) (
) (

=
'
i i
i i
i
x x
x f x f
x f
) ( ) (
) )( (
1
1
1


=
i i
i i i
i i
x f x f
x x x f
x x
Newton-Raphson Method
Approximate the derivative
Secant Method
) ( ) (
) )( (
1
1
1


=
i i
i i i
i i
x f x f
x x x f
x x
Geometric Similar Triangles
f(x)
f(x
i
)
f(x
i-1
)
x
i+1
x
i-1
x
i

X
B
C
E D A
1 1
1
1
) ( ) (
+

+

=

i i
i
i i
i
x x
x f
x x
x f
DE
DC
AE
AB
=
Algorithm for Secant Method
To find a solution to f(x) = 0 given initial guess as x
0
and x
1
.
INPUT: initial guess x0 nd x1, max. number of iterations (N) and
tolerance (TOL).
OUTPUT: approximate solution
Step 1: Set i = 1;
y0 = f(x0); y1 = f(x1);
Step 2: while i N do step 3 to 6
Step 3: Set x
i+1
= x
i
y
i
(x
i
x
i-1
)/(y
i
y
i-1
)
Step 4: If | x
i+1
x
i
| < TOL then
OUTPUT ( x
i+1
); procedure completed successfully
STOP
Step 5: Set i = i+1
Step 6: Set x
i
= x
i+1
; y
i
= f(x
i
);
Step 7: OUTPUT: Method failed after N iterations
STOP
Step 1
0 10 x
1
1

x
- x x
=
i
i i
a
+
+
e
Calculate the next estimate of the root from two initial
guesses
Find the absolute relative approximate error
) ( ) (
) )( (
1
1
1


=
i i
i i i
i i
x f x f
x x x f
x x
Step 2
Find if the absolute relative approximate error is greater
than the pre-specified relative error tolerance.

If so, go back to step 1, else stop the algorithm.

Also check if the number of iterations has exceeded the
maximum number of iterations.
Example
You are working for DOWN THE TOILET COMPANY that
makes floats for ABC commodes. The ball has a
specific gravity of 0.6 and has a radius of 5.5 cm. You
are asked to find the distance to which the ball will get
submerged when floating in water.

Solution
The equation that gives the depth x to which the ball is
submerged under water is given by
Use the Secant method of
finding roots of equations to
find the depth x to which the
ball is submerged under water.
Conduct three iterations to
estimate the root of the above
equation.
( )
4 2 3
10 x 993 3 165 0
-
. + x . - x x f =
Graph of function f(x)
-0.02 0.00 0.02 0.04 0.06 0.08 0.10 0.12
-0.0004
-0.0003
-0.0002
-0.0001
0.0000
0.0001
0.0002
0.0003
0.0004
0.0005


f
(
x
)
x
( )
4
2 3
10 x 993 3
165 0
-
. +
x . - x x f =
Iteration #1
( )( )
( ) ( )
% 61 . 22
0.06461
05 . 0 , 02 . 0
1
1 0
1 0 0
0 1
0 1
= e
=


=
= =

a
x
x f x f
x x x f
x x
x x
Iteration #2
( )( )
( ) ( )
% 525 . 3
06241 . 0
x
06461 . 0 , 05 . 0
2
0 1
0 1 1
1 2
1 0
= e
=


=
= =
a
x
x f f
x x x f
x x
x x
Iteration #3
( )( )
( ) ( )
% 0595 . 0
06238 . 0

6241 . 0 , 06461 . 0
a
3
1 2
1 2 2
2 3
2 1
= e
=


=
= =
x
x f x f
x x x f
x x
x x
Secant Method algorithm

Do while |x
2
- x
1
| >= tolerance value 1
or |f(x
3
)|>= tolerance value 2

Set x
3
= x
2
- f(x
2
)*(x
2
- x
1
)/(f(x
2
)-f(x
1
))

Set x
1
= x
2
;
Set x
2
= x
3
;


END loop
Example 2:
Let us solve the
following problem:

x
4
- x 10 = 0
Then f(x) = x
4
- x 10

df/dx = 4x
3
- 1
Let us first draw a
graph and see where are
the roots;
The roots are between
(-2, -1), & (1, 3)

-3 -2 -1 0 1 2 3
-20
0
20
40
60
80


f
(
x
)
x
Matlab pogram for graph
% --------------- graph of a function
clear all
fun = @(x) x^4. - x - 10.0 ;

xmin = -3.0; xmax = 3.0; max_points = 100;
del = (xmax - xmin)/max_points;
x(1)= xmin ; y(1)=0.0;
for j=2: 1 : max_points
x(j) = x(j-1) + del ; end
for i = 1: max_points
f(i)= fun(x(i)) ; y(i)=0.0 ; end
axis( [xmin xmax -20. 80.0] )
hold on
plot(x, f, x, y, 'LineWidth',2)
Solution
( )( )
( ) ( )
( )
4375 . 3 0 . 10 ) 50 . 1 ( ) 50 . 1 ( ) ( 50 . 1
0 . 8 0 . 8
.) 2 ( 0 . 1 ) 0 . 8 (
0 . 1
0 . 8 0 . 10 .) 1 ( ) 1 ( ) ( , 0 . 1
0 . 8 0 . 10 .) 2 ( ) 2 ( ) ( , 0 . 2
4
1 1
1 0
1 0 0
0 1
4
0 0
4
1 1
= = =


=


=
= = =
+ = = =


x f x
x f x f
x x x f
x x
x f x
x f x
Iteration 1:
( )( )
( ) ( )
( )
2812 . 4 ) ( , 8767 . 1
) 0 . 8 ( 4375 . 3
) 0 . 1 ( 5 . 1 ) 4375 . 3 (
5 . 1
4375 . 3 0 . 10 ) 50 . 1 ( ) 50 . 1 ( ) ( , 50 . 1
0 . 8 0 . 10 .) 1 ( ) 1 ( ) ( , 0 . 1
2 2
0 1
0 1 1
1 2
4
1 1
4
0 0
= =


=


=
= = =
= = =
x f x
x f x f
x x x f
x x
x f x
x f x
Iteration 2:
Solution
( )( )
( ) ( )
( )
5951 . 0 ) ( 6678 . 1
) 4375 . 3 ( 2812 . 4
) 5 . 1 ( 8767 . 1 ) 2812 . 4 (
8767 . 1
2812 . 4 0 . 10 ) 8767 . 1 ( ) 8767 . 1 ( ) ( , 8767 . 1
4375 . 3 0 . 10 ) 50 . 1 ( ) 50 . 1 ( ) ( 50 . 1
3 3
1 2
1 2 2
2 3
4
2 2
4
1 1
= =


=


=
= = =
= = =

x f x
x f x f
x x x f
x x
x f x
x f x
Iteration 3:
Iteration 4:
( )( )
( ) ( )
( )
0857 . 0 ) ( 6933 . 1
) 2812 . 4 ( 5951 . 0
) 8767 . 1 ( 6678 . 1 ) 5951 . 0 (
6678 . 1
5951 . 0 0 . 10 ) 6678 . 1 ( ) 6678 . 1 ( ) ( , 6678 . 1
2812 . 4 0 . 10 ) 8767 . 1 ( ) 8767 . 1 ( ) ( , 8767 . 1
4 4
2 3
2 3 3
3 4
4
2 3
4
2 2
= =


=


=
= = =
= = =
x f x
x f x f
x x x f
x x
x f x
x f x
Solution
( )( )
( ) ( )
0022 . 0 ) ( 6976 . 1
6976 . 1
0857 . 0 0 . 10 ) 6933 . 1 ( ) 6933 . 1 ( ) ( , 6933 . 1
5951 . 0 0 . 10 ) 6678 . 1 ( ) 6678 . 1 ( ) ( , 6678 . 1
4 5
3 4
3 4 4
4 5
4
4 4
4
3 3
= =
=


=
= = =
= = =
x f x
x f x f
x x x f
x x
x f x
x f x
Iteration 5:
Iteration 6:
( )( )
( ) ( )
0001 . 0 ) ( 6975 . 1
6975 . 1
0022 . 0 0 . 10 ) 6976 . 1 ( ) 6976 . 1 ( ) ( , 6976 . 1
0857 . 0 0 . 10 ) 6933 . 1 ( ) 6933 . 1 ( ) ( , 6933 . 1
4 6
4 5
4 5 5
5 6
4
4 5
4
4 4
= =
=


=
= = =
= = =
x f x
x f x f
x x x f
x x
x f x
x f x
The root seems to be -1.6975 with deviation of 0.0001.
Computer Program in MATLAB
% Use of secant method to find the roots of F(x)=0
% Input: xleft,xright = left and right brackets of the root
% n = (optional) number of iterations; default: n =6
% Output: x = estimate of the root
n=8; xleft = -2.0; xright = -1.0;
a = xleft; b =xright; % Copy original bracket to local
variables
fa = a^4 - a - 10.0; % Initial values
fb = b^4 - b - 10.0;
fprintf(' k a b xm f(xm)\n');
for k=1:n
xm = b - fb*((b-a)/(fb-fa)); % Minimize roundoff
fm = xm^4 - xm - 10.0; % f(x) at xm
fprintf('%3d %12.8f %12.8f %12.8f
%12.3e\n',k,a,b,xm,fm);
a = b; fa = fb; b = xm; fb = fm;
end
Results
>>
k a b xm f(xm)
1 -2.00000000 -1.00000000 -1.50000000 -3.438e+000
2 -1.00000000 -1.50000000 -1.87671233 4.282e+000
3 -1.50000000 -1.87671233 -1.66776026 -5.959e-001
4 -1.87671233 -1.66776026 -1.69328962 -8.570e-002
5 -1.66776026 -1.69328962 -1.69757795 2.181e-003
6 -1.69328962 -1.69757795 -1.69747151 -7.683e-006
7 -1.69757795 -1.69747151 -1.69747188 -6.851e-010
8 -1.69747151 -1.69747188 -1.69747188 0.000e+000
>>
Result of the computer program: xleft = -2.0 , xright = -1.0
and iterations n = 8.
The root value is x = -1.69747188
Advantages
Converges fast, if it converges;
Requires two guesses that do not need to bracket the
root.
It is used to refine answers obtained by other
methods such as bisection.
Since this method requires a good first approximation
therefore it gives rapid convergence.
Problems with the Secant Method

This method can be
catastrophic if the
points are near a
point where the first
derivative is zero.
Drawbacks
Division by zero
10 5 0 5 10
2
1
0
1
2
f(x)
prev. guess
new guess
2
2
0
f x ( )
f x ( )
f x ( )
10 10 x x
guess1
, x
guess2
,
( ) ( ) 0 = = x Sin x f
Drawbacks (continued)
Root Jumping
10 5 0 5 10
2
1
0
1
2
f(x)
x'1, (first guess)
x0, (previous guess)
Secant line
x1, (new guess)
2
2
0
f x ( )
f x ( )
f x ( )
secant x ( )
f x ( )
10 10 x x
0
, x
1'
, x , x
1
,
( ) 0 = = Sinx x f

You might also like