CHE 306 Lesson Note 5
CHE 306 Lesson Note 5
ENGINEERING ANALYSIS II
Part 2: Numerical Analysis
A.S. Osunleke,
PhD (Okayama), R.Eng, MNSE, MNSChE
Senior Lecturer, Division of Systems Engineering
Lecture 5
Bisection Method Algorithm
BISECTION METHOD ALGORITHM
1
STEP 2: SET c ( a b) .
2
STOP
SET b=c
ELSE
SET a=c
1
STEP 5: SET c ( a b) ,
2
OUTPUT (c),
STOP
BISECTION METHOD ALGORITHM (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)
disp(' a b c fa fc fb');
fa = f(a);
fb = f(b);
if fa*fb>= 0
end
SAMPLE OF MATLAB CODES (contd)
% bisection loop
c = (a + b)*0.5;
fc = f(c);
disp([a,b,c,fa,fc,fb]);
if fc == 0
return
end
b = c;
fb = fc;
a = c;
fa = fc;
end
end
SAMPLE OF MATLAB CODES (contd)
function y = f(x)
%F(X) Function of X.
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)
This requires
1
ln 6
10
k
ln(2)
ln 106
19.93
ln(2)
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
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
3. x 0.3000x10 4 , y 0.3100x10 4
ERROR ANALYSIS (contd)
Solutions
3. x 0.3000x104 , y 0.3100x104
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
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