0% found this document useful (0 votes)
19 views44 pages

Lecture 11 - Non-Linear Equations (Part 1 - Bracketing Methods)

Uploaded by

muhanndsayed24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
19 views44 pages

Lecture 11 - Non-Linear Equations (Part 1 - Bracketing Methods)

Uploaded by

muhanndsayed24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 44

~ Roots of Equations ~

Bracketing Methods
Chapter 5

Edited by: Dr. wafaa El-Haweet 1


Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Roots of Equations

• Easy
 b  b 2
 4ac
ax  bx  c  0  x 
2

2a

• But, not easy


ax5  bx 4  cx3  dx 2  ex  f  0  x?

• How about these?


sin x  x  0  x?
cos(10 x)  sin( 3x)  0  x?
2
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Nonlinear Equation
Solvers

Bracketing Graphical Open Methods

Bisection Fixed Point Iteration


False Position Newton Raphson
(Regula-Falsi) Secant

All Iterative
3

Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Graphical Approach
• Make a plot of the Using MATLAB, plot f(x)=sin(10x)+cos(3x)
function f(x) and
observe where it
crosses the x-axis,
i.e. f(x) = 0

• Not very practical


but can be used to
obtain rough
estimates for roots
Two distinct
• These estimates can roots between
be used as initial
x= 4.2 and 4.3
guesses for
numerical methods need to be careful
that we’ll study here. 4
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Figure 5.4b
MANY-MANY roots. What do we
do?

Figure 5.4a
f(x)=sin 10x+cos 3x

Figure 5.4c

Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Bracketing:
exceptions

Odd and even


number of roots

6
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Bisection Method

xrnew  xrold
Relative error estimate : e new
100%
x r

Termination criteria: e < etol OR Max.Iteration is reached 9


Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
% Bisection Method - simple
% function f(x) = exp(-x) - x = 0 sample call: bisection(-2, 4, 0.001,500)
MATLAB code
function root = bisection(xl, xu, es, imax);
Bisection if ((exp(-xl) - xl)*(exp(-xu) - xu))>0 % if guesses do not bracket, exit
Method disp('no bracket')
return
end

for i=1:1:imax

xr=(xu+xl)/2; % compute the midpoint xr


ea = abs((xu-xl)/xl); % approx. relative error

test= (exp(-xl) - xl) * (exp(-xr) - xr); % compute f(xl)*f(xr)


• Minimize function
evaluations in the if (test < 0) xu=xr;
code. else xl=xr;
end
Why? if (test == 0) ea=0; end
if (ea < es) break; end
• Because they are
costly (takes more end
time) 10
s=sprintf('\n Root= %f #Iterations = %d \n', xr,i); disp(s);
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
How Many Iterations will It Take?
• Length of the first Interval Lo= xu- xl
• After 1 iteration L1=Lo/2
• After 2 iterations L2=Lo/4
….. …..
• After k iterations Lk=Lo/2k
Lk
• Then we can write: e a  100% e a  e es
x

11
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 1

• If the absolute magnitude of the error is


e es  x 4
 10
100%

and Lo=2, how many iterations will you


have to do to get the required accuracy in
the solution?
4 2
10  k  2 k  2 10 4  k  14.3  15
2

13
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 2
A ball has a specific gravity of 0.6 and has a
radius of 5.5 cm. What is the distance to which
the ball will get submerged when floating in
water.

Figure 6 Diagram of the floating ball 15


Example 1 Cont.
The equation that gives the depth x to which the ball is
submerged under water is given by
x 3  0.165 x 2  3.993 10 4  0

a) Use the bisection 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.
b) Find the absolute relative approximate error at the end of
each iteration, and the number of significant digits at
least correct at the end of each iteration.

16
Example 1 Cont.

From the physics of the problem, the ball would be


submerged between x = 0 and x = 2R,
where R = radius of the ball,
that is
0  x  2R
0  x  20.055
0  x  0.11

Figure 6 Diagram of the floating ball

Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
17
Example 1 Cont.

Solution

To aid in the understanding


of how this method works to
find the root of an equation,
the graph of f(x) is shown to
the right,
where
f x   x3  0.165x 2  3.993 10- 4

Figure 7 Graph of the function f(x)


Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
18
Example 1 Cont.

Let us assume
x  0.00
xu  0.11
Check if the function changes sign between x and xu .
f xl   f 0  0  0.1650  3.993 104  3.993 104
3 2

f xu   f 0.11  0.11  0.1650.11  3.993 104  2.662 104


3 2

Hence

f xl  f xu   f 0 f 0.11  3.993 104  2.662 104  0  
So there is at least on root between x and xu, that is between 0 and 0.11

Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
19
Example 1 Cont.

Figure 8 Graph demonstrating sign change between initial limits


Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
20
Example 1 Cont.

Iteration 1 x  xu 0  0.11
The estimate of the root is xr    0.055
2 2

f xr   f 0.055  0.055  0.1650.055  3.993 104  6.655 105


3 2


f xl  f xr   f 0 f 0.055  3.993 10 4 6.655 105  0  
Hence the root is bracketed between xr and xu , that is, between 0.055
and 0.11. So, the lower and upper limits of the new bracket are
xl  0.055, xu  0.11
At this point, the absolute relative approximate error a cannot be
calculated as we do not have a previous approximation.

Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
21
Example 1 Cont.

Figure 9 Estimate of the root for Iteration 1


Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
22
Example 1 Cont.

Iteration 2 x  xu 0.055  0.11


The estimate of the root is xr    0.0825
2 2
f xr   f 0.0825  0.0825  0.1650.0825  3.993 104  1.622 104
3 2


f xl  f xr   f 0.055 f (0.0825)   1.622 104 6.655 105  0  
Hence the root is bracketed between x and xr , that is, between 0.055
and 0.0825. So, the lower and upper limits of the new bracket are
xl  0.055, xu  0.0825

Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
23
Example 1 Cont.

Figure 10 Estimate of the root for Iteration 2


Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
24
Example 1 Cont.

The absolute relative approximate error a at the end of Iteration 2 is


xrnew  xrold
a  new
100
xr
0.0825  0.055
  100
0.0825
 33.333%

None of the significant digits are at least correct in the estimate root of
xr = 0.0825 because the absolute relative approximate error is greater
than 5%.

Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
25
Example 1 Cont.

Iteration 3 x  xu 0.055  0.0825


The estimate of the root is xr    0.06875
2 2
f xr   f 0.06875  0.06875  0.1650.06875  3.993 104  5.563 105
3 2


f xl  f xr   f 0.055 f 0.06875  6.655 105  5.563 105  0  
Hence the root is bracketed between x and xr , that is, between 0.055
and 0.06875. So, the lower and upper limits of the new bracket are
xl  0.055, xu  0.06875

Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
26
Example 1 Cont.

Figure 11 Estimate of the root for Iteration 3


Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 27
Example 1 Cont.

The absolute relative approximate error a at the end of Iteration 3 is


xrnew  xrold
a  new
100
xr
0.06875  0.0825
  100
0.06875
 20%

Still none of the significant digits are at least correct in the estimated
root of the equation as the absolute relative approximate error is
greater than 5%.
Seven more iterations were conducted and these iterations are shown in
Table 1.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
28
Table 1 Cont.
Table 1 Root of f(x)=0 as function of number of iterations for
bisection method.
Iteration x xu Xr a % f(xr)

1 0.00000 0.11 0.055 ---------- 6.655×10−5


2 0.055 0.11 0.0825 33.33 −1.622×10−4
3 0.055 0.0825 0.06875 20.00 −5.563×10−5
4 0.055 0.06875 0.06188 11.11 4.484×10−6
5 0.06188 0.06875 0.06531 5.263 −2.593×10−5
6 0.06188 0.06531 0.06359 2.702 −1.0804×10−5
7 0.06188 0.06359 0.06273 1.370 −3.176×10−6
8 0.06188 0.06273 0.0623 0.6897 6.497×10−7
9 0.0623 0.06273 0.06252 0.3436 −1.265×10−6
10 0.0623 0.06252 0.06241 0.1721 −3.0768×10−7

Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
29
Table 1 Cont.

Hence the number of significant digits at least correct is given by the


largest value or m for which
a  0.5  10 2 m
0.1721  0.5  10 2 m
0.3442  10 2 m
log0.3442  2  m
m  2  log0.3442  2.463
So
m2
The number of significant digits at least correct in the estimated root
of 0.06241 at the end of the 10th iteration is 2.
30
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Bisection Method

Pros Cons

• Easy • Slow
• Always finds a root • Need to find initial
• Number of iterations guesses for xl and xu
required to attain an • No account is taken
absolute error can be of the fact that if f(xl)
computed a priori. is closer to zero, it is
likely that root is
closer to xl .

31
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Drawbacks (continued)

• If a function f(x) is such that it just


touches the x-axis it will be unable to find
the lower and upper guesses.
f(x)

f x   x 2

34
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Drawbacks (continued)

 Function changes sign but root does not


exist

f x  
f(x)
1
x
x

35
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
The False-Position Method (Regula-Falsi)

• We can approximate the


solution by doing a
linear interpolation
between f(xu) and f(xl)

• Find xr such that


l(xr)=0, where l(x) is the
linear approximation of
f(x) between xl and xu

• Derive xr using similar


triangles

xl f u  xu f l
xr 
fu  fl
36
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Procedure
1. Find a pair of values of x, xl and xu such that
fl=f(xl) <0 and fu=f(xu) >0.
2. Estimate the value of the root from the
following formula
xl f u  xu f l
xr 
fu  fl
and evaluate f(xr).

37

Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
3. Use the new point to replace one of the
original points, keeping the two points on
opposite sides of the x axis.

If f(xr)<0 then xl=xr == > fl=f(xr)

If f(xr)>0 then xu=xr == > fu=f(xr)

If f(xr)=0 then you have found the root and


need go no further!

38
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
4. See if the new xl and xu are close enough for
convergence to be declared. If they are not go back
to step 2.

• Why this method?


– Faster
– Always converges for a single root.

Pitfalls of the False-Position Method


Note: Always check by substituting estimated
root in the original equation to determine
whether f(xr) ≈ 0. 39

Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
The False-Position Method
Works well, but not always!
 Here is a pitfall 

Modified False-Position
One way to mitigate the “one-sided”
nature of the false position (i.e. the
pitfall case) is to have the algorithm
detect when one of the bounds is
stuck.

If this occurs, then the original


formula xr = (xl + xu)/2 can be used

40
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
False-position
(Regula-Falsi)

Linear
Linear interpolation

Interpolation
Method

41
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
False-Position (Linear Interpolation) Method
Manning Equation
>> false_position('manning')
enter lower bound xl = 0
enter upper bound xu = 10
allowable tolerance es = 0.00001
maximum number of iterations maxit = 50
False position method has converged
step xl xu xr f(xr)
1.0000 0 10.0000 4.9661 271.4771
2.0000 4.9661 10.0000 6.0295 27.5652
3.0000 6.0295 10.0000 6.1346 2.4677
4.0000 6.1346 10.0000 6.1440 0.2184
5.0000 6.1440 10.0000 6.1449 0.0193
6.0000 6.1449 10.0000 6.1449 0.0017
7.0000 6.1449 10.0000 6.1449 0.0002

 Much faster convergence than the bisection method


 May be slower than bisection method for some cases 42
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Convergence Rate
• Why don't we always use false
position method?
• There are times it may converge very,
very slowly.
• Example:
f ( x )  x  3x  4  0 4

• What other methods can we use?

43
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
f ( x)  x 4  3 x  4  0
xl , xu   0, 3
Convergence
slower than
bisection method

1 23 2 1
root midpoint

44
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Bisection Method False-Position Method
» xl = 0; xu = 3; es = 0.00001; maxit = 100; » xl = 0; xu = 3; es = 0.00001; maxit = 100;
» [xr,fr]=bisect2(inline(‘x^4+3*x-4’)) » [xr,fr]=false_position(inline(‘x^4+3*x-4’))
Bisection method has converged False position method has converged
step xl xu xr f(x) step xl xu xr f(xr)
1.0000 0 3.0000 1.5000 5.5625 1.0000 0 3.0000 0.1333 -3.5997
2.0000 0 1.5000 0.7500 -1.4336 2.0000 0.1333 3.0000 0.2485 -3.2507
3.0000 0.7500 1.5000 1.1250 0.9768 3.0000 0.2485 3.0000 0.3487 -2.9391
4.0000 0.7500 1.1250 0.9375 -0.4150 4.0000 0.3487 3.0000 0.4363 -2.6548
5.0000 0.9375 1.1250 1.0312 0.2247 5.0000 0.4363 3.0000 0.5131 -2.3914
6.0000 0.9375 1.0312 0.9844 -0.1079 6.0000 0.5131 3.0000 0.5804 -2.1454
7.0000 0.9844 1.0312 1.0078 0.0551 7.0000 0.5804 3.0000 0.6393 -1.9152
8.0000 0.9844 1.0078 0.9961 -0.0273 8.0000 0.6393 3.0000 0.6907 -1.7003
9.0000 0.9961 1.0078 1.0020 0.0137 9.0000 0.6907 3.0000 0.7355 -1.5010
10.0000 0.9961 1.0020 0.9990 -0.0068 10.0000 0.7355 3.0000 0.7743 -1.3176
11.0000 0.9990 1.0020 1.0005 0.0034 11.0000 0.7743 3.0000 0.8079 -1.1503
12.0000 0.9990 1.0005 0.9998 -0.0017 12.0000 0.8079 3.0000 0.8368 -0.9991
13.0000 0.9998 1.0005 1.0001 0.0009 13.0000 0.8368 3.0000 0.8617 -0.8637
14.0000 0.9998 1.0001 0.9999 -0.0004 14.0000 0.8617 3.0000 0.8829 -0.7434
15.0000 0.9999 1.0001 1.0000 0.0002 15.0000 0.8829 3.0000 0.9011 -0.6375
16.0000 0.9999 1.0000 1.0000 -0.0001 16.0000 0.9011 3.0000 0.9165 -0.5448
17.0000 1.0000 1.0000 1.0000 0.0001 17.0000 0.9165 3.0000 0.9296 -0.4642
18.0000 1.0000 1.0000 1.0000 0.0000 18.0000 0.9296 3.0000 0.9408 -0.3945
19.0000 1.0000 1.0000 1.0000 0.0000 19.0000 0.9408 3.0000 0.9502 -0.3345
20.0000 0.9502 3.0000 0.9581 -0.2831
….
40.0000 0.9985 3.0000 0.9988 -0.0086
f ( x )  x  3x  4  0
4
….
58.0000 0.9999 3.0000 0.9999 -0.0004
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example: Rate of Convergence
» x = -2:0.1:2; y = x.^3-3*x+1; z = x*0;
» H = plot(x,y,'r',x,z,'b'); grid on; set(H,'LineWidth',3.0);
» xlabel('x'); ylabel('y'); title('f(x) = x^3 - 3x + 1 = 0');

46
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
>> bisect2(inline('x^3-3*x+1')) Comparison of rate of
enter lower bound xl = 0 convergence for
enter upper bound xu = 1
allowable tolerance es = 1.e-20 bisection and false-
maximum number of iterations maxit = 100 position method
exact zero found
step xl xu xr f(xr)
1.0000 0 1.0000 0.5000 -0.3750
2.0000 0 0.5000 0.2500 0.2656
3.0000 0.2500 0.5000 0.3750 -0.0723
4.0000 0.2500 0.3750 0.3125 0.0930
5.0000 0.3125 0.3750 0.3438 0.0094
6.0000 0.3438 0.3750 0.3594 -0.0317
7.0000 0.3438 0.3594 0.3516 -0.0112
8.0000 0.3438 0.3516 0.3477 -0.0009
9.0000 0.3438 0.3477 0.3457 0.0042
10.0000 0.3457 0.3477 0.3467 0.0016
11.0000 0.3467 0.3477 0.3472 0.0003
12.0000 0.3472 0.3477 0.3474 -0.0003
13.0000 0.3472 0.3474 0.3473 0.0000
14.0000 0.3473 0.3474 0.3474 -0.0001
.. .
.. .
50.0000 0.3473 0.3473 0.3473 -0.0000
51.0000 0.3473 0.3473 0.3473 0.0000
52.0000 0.3473 0.3473 0.3473 -0.0000
53.0000 0.3473 0.3473 0.3473 -0.0000
54.0000 0.3473 0.3473 0.3473 0

Continued on next page 47


Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
>> false_position(inline('x^3-3*x+1'))
enter lower bound xl = 0
enter upper bound xu = 1 f(x) = x3 – 3x +1 = 0
allowable tolerance es = 1.e-20
maximum number of iterations maxit = 100
exact zero found
step xl xu xr f(xr)
1.0000 0 1.0000 0.5000 -0.3750
2.0000 0 0.5000 0.3636 -0.0428
3.0000 0 0.3636 0.3487 -0.0037
4.0000 0 0.3487 0.3474 -0.0003
5.0000 0 0.3474 0.3473 -0.0000
6.0000 0 0.3473 0.3473 -0.0000
7.0000 0 0.3473 0.3473 -0.0000
8.0000 0 0.3473 0.3473 -0.0000
9.0000 0 0.3473 0.3473 -0.0000
10.0000 0 0.3473 0.3473 -0.0000
11.0000 0 0.3473 0.3473 -0.0000
12.0000 0 0.3473 0.3473 -0.0000
13.0000 0 0.3473 0.3473 -0.0000
14.0000 0 0.3473 0.3473 -0.0000
15.0000 0 0.3473 0.3473 -0.0000
16.0000 0 0.3473 0.3473 -0.0000
17.0000 0 0.3473 0.3473 0 Compute
iter1=length(x1); iter2=length(x2); k1=1:iter1; k2=1:iter2;
relative
>> root1=x1(iter1); root2=x2(iter2); errors
>> error1=abs((x1-root1)/root1); error2=abs((x2-root2)/root2);
>> H=semilogy(k1,error1,'ro-',k2,error2,'bs-'); set(H,'LineWidth',2.0);
>> xlabel('Number of Iterations'); ylabel('Relative Error');
48
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Rate of Convergence
f(x)= x3  3x + 1

Bisection
Method

False
position

49
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
How to find good initial guesses?
• Start at one end of the region of interest (xa) and evaluate
f(xa), f(xa+Dx), f(xa+2Dx), f(xa+3Dx), ........

• Continue until the sign of the result changes.


If that happens between f(xa+k*Dx) and f(xa+(k+1)*Dx)

then pick xl= xa+k*Dx and xu= xa+(k+1)*Dx

Problem:
if Dx is too small  search is very time consuming
if Dx is too large  could jump over two closely spaced roots

Suggestions:
• Generate random x values and evaluate f(x) each time until you find two values that
satisfy f(x1)*f(x2) < 0
• Know the application and plot the function to see the location of the roots, and pick xl
and xu accordingly to start the iterations.
50
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

You might also like