0% found this document useful (0 votes)
1K views

Lab Experiment # 2: Root Finding Using Bisection & False Ppsition Methods

This document describes a lab experiment on using the bisection and false position root-finding methods. Students are asked to find roots of the equation f(x)=cos(x)-sin(3x)=0 using MATLAB code implementing the bisection method. They must run the code with different initial intervals and error tolerances and record the results. The objectives are to understand how to use the bisection method to find roots, analyze results with different starting values and tolerances, and implement the method in MATLAB.

Uploaded by

qais alnawafah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Lab Experiment # 2: Root Finding Using Bisection & False Ppsition Methods

This document describes a lab experiment on using the bisection and false position root-finding methods. Students are asked to find roots of the equation f(x)=cos(x)-sin(3x)=0 using MATLAB code implementing the bisection method. They must run the code with different initial intervals and error tolerances and record the results. The objectives are to understand how to use the bisection method to find roots, analyze results with different starting values and tolerances, and implement the method in MATLAB.

Uploaded by

qais alnawafah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB EXPERIMENT # 2: ROOT FINDING USING BISECTION & FALSE

PPSITION METHODS

Name: Grade (5)

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

Figure 1: Graphical description of the Figure 2: Graphical description of the


Bisection method. False Position method.

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 2: An estimate of the root xr is determined by:

for Bisection method and for False Position method.

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:

Defining the approximation error as

𝑥𝑟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

Preliminary Work (to be done before lab session)

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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Initialize iteration counter


iter = 0;

%old estimate of root is unknown


xrold = Inf;

%display string
disp(' iteration xl xu xr f(xl) f(xu) f(xr)
ea(%)');

while (1)

%Bisection method
xr = (xl + xu)/2;

%compute percentage relative approximate


error if(xr ~= 0)
ea = abs( (xr - xrold) / xr)* 100;
end

%display some results


disp([iter, xl, xu, xr, func(xl), func(xu), func(xr), ea]);

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

%update old estimate of the root


xrold = xr;

%update the iteration counter


iter = iter + 1;

%terminate the loop if solution is


found if( (ea < es) || (iter >= imax))
break;
end
end

4. The implementation of the function 𝑓(𝑥)= cos(𝑥)−sin(3𝑥) = 0 is given below.

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.

Iteration xl xu xr f(xl) f(xu) f(xr) ℇa (%)


0
1
2
3
4
5

2. Repeat step 1 until percentage approximate relative error (ℇa (%)) is less than ℇs = 0.2% .

Iteration xl xu xr f(xl) f(xu) f(xr) ℇa (%)


0
1
2
3
4
5
6
7
8
9

5
3. Repeat step 1 using the initial interval [-3, -2.5]

Iteration xl xu xr f(xl) f(xu) f(xr) ℇa (%)


0
1
2
3

Discussion and Analysis

1. How does the choice of the initial interval affect the solution?

2. Discuss the convergence of the Bisection method.

3. How the speed of convergence and the error tolerance (s) are related?

You might also like