0% found this document useful (0 votes)
12 views

CHE 306 Lesson Note 5

Uploaded by

Sysavane Usman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

CHE 306 Lesson Note 5

Uploaded by

Sysavane Usman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CHE 306

ENGINEERING ANALYSIS II
Part 2: Numerical Analysis

A.S. Osunleke,
PhD (Okayama), R.Eng, MNSE, MNSChE
Senior Lecturer, Division of Systems Engineering

Department of Chemical Engineering, O.A.U.,


Ile-Ife

Lecture 5
Bisection Method Algorithm
BISECTION METHOD ALGORITHM

INPUT: Nontrivial Bracket - [a, b], Tolerance – ε.

OUTPUT: Approximate solution, c.

STEP 1: WHILE a  b   do STEPS 2-4.

1
STEP 2: SET c  ( a  b) .
2

STEP 3: IF f(c) = 0, THEN OUTPUT (c),

STOP

STEP 4: IF f(a) x f(c) < 0 THEN

SET b=c

ELSE

SET a=c

1
STEP 5: SET c  ( a  b) ,
2
OUTPUT (c),
STOP
BISECTION METHOD ALGORITHM (MATLAB PROGRAM)

function c = bisect(a, b, epsilon)

%BISECT - Bisection method for f(x) = 0.

% BISECT (A, B, EPSILON) is an approximate solution on the interval [a, b].

% The final subinterval has length < = EPSILON

NOTES on Bisection MATLAB Program

1. LINE 1: begins with keyword function followed by output argument c and the “=”
symbol. This is followed by the input arguments within round brackets.

2. LINE 2: called the H1 (help 1) line. It should be a comment line of special form:
beginning with %, followed without space by the function name in capitals, followed
by one or more spaces and then a brief description. The description should begin with
a capital, end with a period “.” and omit the words ‘the’ and ‘a’.
BISECTION METHOD ALGORITHM (MATLAB PROGRAM)

3. All comments lines from H1 up to the first non-comment line (usually a blank line for
readability) are displayed when “ helpfunction_Name” is typed in the command
window after the “ >> “ symbol. These lines should describe the function and its
arguments (capitalized by convention).

Note that the function name must be the same as the name of the m-file in which it is stored.
So here “bisect” and “bisect.m” for the m-file.
SAMPLE OF MATLAB CODES
function c = bisect(a,b,epsilon)

%BISECT Bisection method for f(x) = 0.

% BISECT(A,B,EPSILON) is an approximate solution on the

% interval [A,B]. The final subinterval has length <= EPSILON.

disp(' a b c fa fc fb');

fa = f(a);

fb = f(b);

% check initial bracket

if fa*fb>= 0

error('fa*fb is non-negative! The bracket is out of range!!!')

end
SAMPLE OF MATLAB CODES (contd)
% bisection loop

while abs(b-a) > epsilon

c = (a + b)*0.5;

fc = f(c);

disp([a,b,c,fa,fc,fb]);

if fc == 0

return

end

if fa*fc < 0 % root is to left of mid-point

b = c;

fb = fc;

else % root is to right of mid-point

a = c;

fa = fc;

end

end
SAMPLE OF MATLAB CODES (contd)

function y = f(x)

%F(X) Function of X.

% Y = F(X) computes given function of X.

% F(X) = x^2 - 0.5.

y = x^2 - 0.5;
INTERVAL LENGTH
If the original bracket has length L0 i.e L0  b  a , then the bracket length after k steps
L0
(calculation of k brackets after the original) is Lk  . This formula allows us to calculate
2k
the number of steps required to make the bracket length Lk E . Thus, Lk E implies

L0
k
E
2
L
2k  0
E
L 
ln  0 
E
k  
ln(2)

Example 2: If L0 1 and E 10 6 , calculate the number of iterations required to make


Lk E .
INTERVAL LENGTH (Contd)

This requires

 1 
ln   6 
10 
k 
ln(2)
ln 106 
 19.93
ln(2)

Thus k 20 (k is an integer) iterations are required.

Note: useful numbers: 2 10 ~ 10 3 , 2 20 ~ 10 6 .

Note: the ‘error’ in the mid-point as an approximation to the root introduces an extra ½,
Ck  P 1/ 2 Lk .
ERROR ANALYSIS
The Bisection algorithm was stopped when b  a E . This gave us an error estimate for the
approximate root. If the approximate root is ≈1, then an error estimate of 10 6 , say, is
reasonable. If however, the approximate root is ≈ 10 7 , then an error estimate of 10 6 is very
bad.

ABSOLUTE ERROR

If y approximates x, then y  x is the absolute error.

RELATIVE ERROR

y x
If y approximates x, then is the relative error. This definition requires x 0 . Often
x
relative error is multiplied by 100 to give percentage error.
CLASS WORK
If y approximates x, then compute the (i) Absolute Error, (ii) Relative Error and

(iii) Percentage Error for the following:

1. x 0.3000 x101 , y 0.3100 x101 .

2. x 0.3000 x10 3 , y 0.3100 x10  3

3. x 0.3000x10 4 , y 0.3100x10 4
ERROR ANALYSIS (contd)
Solutions

1. x 0.3000 x101 , y 0.3100 x101

Absolute Error 0.1 .

Relative error 0.3333x10 1

2. x 0.3000 x10 3 , y 0.3100 x10 3

Absolute Error 0.1x10 4

Relative error 0.3333x10 1

3. x 0.3000x104 , y 0.3100x104

Absolute Error 0.1x103 . Relative error 0.3333x10 1

Observation: Widely varying absolute errors for the same relative error.

Generally, relative error is more meaningful while absolute error can be misleading.
ERROR ANALYSIS (contd)

STOPPING CRITERIA

In the Bisection algorithm, the computation is ‘stopped’ when b  a E . Then the current
midpoint c is an approximation to the root p with

Absolute Error  c  P 1/ 2 a  b

i.e. half the current interval length. Thus this stopping criterion gives an approximation with
absolute error 1/ 2E .
ADVANTAGES AND DISADVANTAGES OF THE BISECTION METHOD

Disadvantages

1. Slow convergence, a large number of iterations may be required to make the absolute
error, c  P , small.
2. Good intermediate approximations may be discarded when the root is close to the
midpoint.

Advantages

1. Always converge to a solution. This makes it an attractive technique for finding a


‘starting value’ (initial guess) for faster methods.
2. Does not require derivative of a given function.

You might also like