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

Laboratory 1

This document provides instructions for a lab experiment on using the bisection method to find roots of equations. It includes the objectives, algorithm, pseudocode, Scilab implementation, and steps for students to perform. Students will use the bisection method code provided to find roots of the function f(x)=cos(x)-sin(3x) with different initial intervals and error tolerances. They will record the results in tables and analyze how the initial interval and error tolerance affect the convergence of the bisection method.

Uploaded by

Hannan Maruhom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
254 views

Laboratory 1

This document provides instructions for a lab experiment on using the bisection method to find roots of equations. It includes the objectives, algorithm, pseudocode, Scilab implementation, and steps for students to perform. Students will use the bisection method code provided to find roots of the function f(x)=cos(x)-sin(3x) with different initial intervals and error tolerances. They will record the results in tables and analyze how the initial interval and error tolerance affect the convergence of the bisection method.

Uploaded by

Hannan Maruhom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ES 84

Numerical Methods

LAB SHEETS

STUDENT WORKBOOK
LAB EXPERIMENT # 1: ROOT FINDING USING BISECTION METHOD

Student ID Name Date Performed

Objectives
1. To determine roots of an equation in single variable using Bisection method.

2. To understand the SCILAB implementation of the Bisection method.

3. To analyze of results using different initial values and error tolerance.

Algorithm

Figure 1: Graphical description of the Bisection method.

ES 84 Numerical Methods Laboratory Page 2


Step 1: Choose lower 𝑥𝑙 and upper 𝑥𝑢 guesses for the root such that the function changes sign over
the interval. This can be checked by ensuring that 𝑓(𝑥𝑙 )𝑓(𝑥𝑢 ) < 0.
Step 2: An estimate of the root 𝑥𝑟 is determined by

𝑥𝑙 + 𝑥𝑢
𝑥𝑟 =
2

Step 3: Make the following evaluations to determine in which subinterval the root lies

(a) If 𝑓(𝑥𝑙 )𝑓(𝑥𝑢 ) < 0, the root lies in the lower subinterval. Therefore, set xu = 𝑥𝑟 and
return to step 2.

(b) If 𝑓(𝑥𝑙 )𝑓(𝑥𝑢 ) > 0, the root lies in the upper subinterval. Therefore, set 𝑥𝑙 = 𝑥𝑟 and return
to step 2.

(c) If 𝑓(𝑥𝑙 )𝑓(𝑥𝑢 ) = 0, the root equals 𝑥𝑟 , terminate the computation.


Termination Criteria:
Defining the approximation error as

𝑥𝑟𝑛𝑒𝑤 − 𝑥𝑟𝑜𝑙𝑑
𝜀𝑎 = | | 100%
𝑥𝑟𝑛𝑒𝑤

𝑥𝑟𝑛𝑒𝑤 𝑖𝑠 𝑡ℎ𝑒 𝑟𝑜𝑜𝑡 𝑜𝑓 𝑡ℎ𝑒 𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑖𝑡𝑒𝑟𝑎𝑡𝑖𝑜𝑛 𝑎𝑛𝑑 𝑥𝑟𝑜𝑙𝑑 𝑖𝑠 𝑡ℎ𝑒 𝑟𝑜𝑜𝑡 𝑜𝑓 𝑡ℎ𝑒 𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 𝑖𝑡𝑒𝑟𝑎𝑡𝑖𝑜𝑛

Then the computations will be terminated if

𝜀𝑎 (%) < 𝜀𝑠 𝑜𝑟 𝑚𝑎𝑥𝑖𝑚𝑢𝑚 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑖𝑡𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝑠 𝑖𝑠 𝑟𝑒𝑎𝑐ℎ𝑒𝑑

ES 84 Numerical Methods Laboratory Page 3


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

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.

3. Understand the algorithm, the corresponding pseudo-code and Scilab code of the Bisection
method.

ES 84 Numerical Methods Laboratory Page 4


Scilab 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
printf("Iteration \t Xr \t ea\n") ;
while (1) //this loop is ALWAYS true but will be stopped by the stopping criterion

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

//compute percentage relative approximate error


if (xr~=0) then
ea = abs((xr - xrold)/xr) * 100;
end
//print the results for each iteration
printf("%d \t %f \t %f\n",iter,xr,ea) ;

//test each interval for root and update xl and/or xu.


test = f(xl) * f(xr); //we use the f(x) function which will be defined in a
separate function
if( test < 0)
xu = xr;
elseif (test > 0) then
xl = xr;
else
ea = 0;
end

//update old estimate of the root


xrold = xr;

//update the iteration counter


iter = iter + 1;

//stopping criterion
//terminate the loop if solution is found or if maximum no. iteration is
reached
if (ea < es) | (iter >= imax)
break; //end loop
end

end

endfunction

ES 84 Numerical Methods Laboratory Page 5


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

function y=f(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);
endfunction

Combine the two codes above and execute in the scilab console window:

For interval -4 and 4 with a stopping criterion of 0.05% and 30 maximum number of iterations

Or

For interval 0 and 0.5 with a stopping criterion of 0.2% and 30 maximum number of iterations

ES 84 Numerical Methods Laboratory Page 6


Lab Work
1. Consider the equation 𝑓(𝑥) = cos(𝑥) − sin(3𝑥) = 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 (𝜀𝑎 (%)) is less than 𝜀𝑠 = 2%) . Fill the following table.
Iteration 𝒙𝒍 𝒙𝒖 𝒙𝒓 𝒇(𝒙𝒍 ) 𝒇(𝒙𝒖 ) 𝒇(𝒙𝒓 ) 𝜺𝒂 (%)
0
1
2
3
4
5

2. Repeat step 1 until percentage approximate relative error ( 𝜀𝑎 (%)) is less than 𝜀𝑠 = 0.2%).

Iteration 𝒙𝒍 𝒙𝒖 𝒙𝒓 𝒇(𝒙𝒍 ) 𝒇(𝒙𝒖 ) 𝒇(𝒙𝒓 ) 𝜺𝒂 (%)


0
1
2
3
4
5
6
7
8
9

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

Iteration 𝒙𝒍 𝒙𝒖 𝒙𝒓 𝒇(𝒙𝒍 ) 𝒇(𝒙𝒖 ) 𝒇(𝒙𝒓 ) 𝜺𝒂 (%)


0
1
2
3

ES 84 Numerical Methods Laboratory Page 7


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 (𝜀𝑠 ) are related?

ES 84 Numerical Methods Laboratory Page 8

You might also like