Lab Experiment # 2: Root Finding Using Bisection & False Ppsition Methods
Lab Experiment # 2: Root Finding Using Bisection & False Ppsition Methods
PPSITION METHODS
ID Number:
Lab Section:
Objectives
1. To determine roots of an equation in single variable using Bisection method.
2. To understand the MATLAB implementation of the Bisection method.
3. To analyze of results using different initial values and error tolerance.
Algorithm
Step 1: Choose lower xl and upper xu guesses for the root such that the function changes
sign over the interval. This can be checked by ensuring that f(xl)f(xu) < 0.
Step 3: Make the following evaluations to determine in which subinterval the root lies
1
(a) If f(xl)f(xr) < 0, the root lies in the lower subinterval. Therefore, set xu = xr and return
to step 2.
(b) If f(xl)f(xr) > 0, the root lies in the upper subinterval. Therefore, set xl = xr and return
to step 2.
(c) If f(xl)f(xr) = 0, the root equals xr, terminate the computation.
Termination Criteria:
𝑥𝑟new 𝑖𝑠 𝑡ℎ𝑒 𝑟𝑜𝑜𝑡 𝑜𝑓 𝑡ℎ𝑒 𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑖𝑡𝑒𝑟𝑎𝑡𝑖𝑜𝑛 𝑎𝑛𝑑 𝑥𝑟𝑜𝑙𝑑 𝑖𝑠 𝑡ℎ𝑒 𝑟𝑜𝑜𝑡 𝑜𝑓 𝑡ℎ𝑒 𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 𝑖𝑡𝑒𝑟𝑎𝑡𝑖𝑜𝑛
Then the computations will be terminated if 𝜀𝑎(%) < 𝜀𝑠 𝑜𝑟 𝑚𝑎𝑥𝑖𝑚𝑢𝑚 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑖𝑡𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝑠 𝑖𝑠
𝑟𝑒𝑎𝑐ℎ𝑒𝑑.
Pseudocode
FUNCTION Bisect(xl, xu, es, imax)
iter = 0
xrold = ∞
DO
xr = (xl + xu)/2
IF xr ≠ 0 THEN
ea = ABS((xr – xrold)/xr)*100
END IF
DISPLAY xl, xu, xr, f(xl), f(xu), f(xr), ea
test = f(xl)*f(xr)
IF test < 0 THEN
xu = xr
ELSE IF test > 0 THEN
xl = xr
ELSE
ea = 0
END IF
xrold = xr
iter = iter + 1
IF ea < es OR iter ≥ imax EXIT
END DO
Bisect = xr
END Bisect
2
Practical Work
1. Consider the function 𝑓(𝑥)= cos(𝑥)−sin(3𝑥) = 0. Plot the graph of the function in the interval [-4,
4].
2. Find a root of the function using the initial interval [0, 0.5]. Perform 5 iterations of the Bisection
method, and another 5 iterations of False Position method.
3. Understand the algorithm, the corresponding pseudo-code and MATLAB code of the Bisection
method.
MATLAB Implementation
function [xr, ea] = bisect(xl, xu, es, imax)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function implements the bisection algorithm for finding roots
% of equation in single variable
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Syntax: [xr, ea] = bisect(xl, xu, es, imax)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Input(s):
% xl = Lower guess
% xu = Upper guess
% es = Percentage value of error tolerance
% imax = Maximum number of iterations
% Output(s):
% xr = Estimated value of root at last iteration
% ea = Percentage relative approximate error at last iteration
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%display string
disp(' iteration xl xu xr f(xl) f(xu) f(xr)
ea(%)');
while (1)
%Bisection method
xr = (xl + xu)/2;
3
%test each interval for root and update xl and/or xu.
test = func(xl)*func(xr);
if(test < 0)
xu = xr;
elseif(test > 0)
xl = xr;
else
ea = 0;
end
function y = func(x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function implements the equation f(x) = cos(x) - sin(3x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Input(s):
% x = input argument for the function
% Output(s):
% y = function value at x
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y = cos(x) - sin(3*x);
end
4
Lab Work
1. Consider the equation f(x) = cos (x) − sin (3x) = 0 which has many roots. Use the above
code to find a root in the initial interval [0, 0.5]. Perform the computations until percentage
approximate relative error (ℇa (%)) is less than ℇs = 2%. Fill the following table.
2. Repeat step 1 until percentage approximate relative error (ℇa (%)) is less than ℇs = 0.2% .
5
3. Repeat step 1 using the initial interval [-3, -2.5]
1. How does the choice of the initial interval affect the solution?
3. How the speed of convergence and the error tolerance (s) are related?