You are on page 1of 6

Numerical Analysis Lab

Lab No.9

Submitted By:
Muhammad Hassan (2020-MC-274)
Submitted to:
Dr. Arshi Khalid
--------------------------------------------------------------------------------------------------------
-

Department of Mechatronics & Control Engineering


University of Engineering & Technology Lahore,
Faisalabad Campus.
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid

Date: 24-11-2022

Lab Objective:
Implement the following Root Finding Methods on the Matlab.

 Bisection Method
 Bisection Method when only Function is given (and program finds the bracket values itself)
 False Position Method

 Bisection Method:
The bisection method is used to find the  roots of a polynomial equation. It separates the interval
and subdivides the interval in which the root of the equation lies.

The principle behind this method is the intermediate theorem for continuous functions. It works by
narrowing the gap between the positive and negative intervals until it closes in on the correct answer. This
method narrows the gap by taking the average of the positive and negative intervals. It is a simple method
and it is relatively slow.

Figure 9.1: Graph to Understand Bisection Method

Bisection Method Algorithm:


Following are the steps to calculate the root of any continuous function f(x).

a. Find two points, say a and b such that a < b and f(a)* f(b) < 0
b. Find the midpoint of a and b, say “x_new”. x_new is the root of the given function and if f(t) = 0
then stop, otherwise we continue.
c. Divide the interval [a,b].
1. If f(x_new)*f(a) <0, there exist a root between x_new and a.
2. Else if f(x_new) *f (b) < 0, there exist a root between x_new and b.
d. Repeat above three steps until f(x_new) ≈ 0 or we are provided to do a specified number of iterations
or there is no change in f(x_new).

2
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid

Matlab Code:
% Root Finding Methods Bisectioning Method
clearvars;
clc;
% to take the function, a and b as input from the user
f = input('Enter the Function: ');
a = input('Enter the value of a: ');
b = input('Enter the value of b: ');
% Bisection Method
x_new = (a+b)/2;
fx = f(x_new);
while ((fx > 0.00001) || (fx < -0.00001))
fa = f(a);
fb = f(b);
if (fx < 0) && (fa <0)
a = x_new;
elseif (fx>0) && (fa>0)
a = x_new;
elseif (fx<0) && (fb<0)
b = x_new;
elseif (fx>0) && (fb>0)
b = x_new;
end
x_new = (a+b)/2;
fx = f(x_new);
end
% to print the root of the function
fprintf('\nRoot of the input function is = %f\n',x_new);

Output:
If we input
Enter the Function: @(x) x^3-x-1
Enter the value of a: 1
Enter the value of b: 2
Then
Root of the input function is = 1.324718

 Bisection Method when only Function is given (and program finds the bracket
values itself)

The Following program takes the function as in input from the user and finds out the values of bracket
(i.e. a &b) itself.
Matlab Code:
% Root Finding Methods Bisection Method When Brackett is not given
clearvars;
clc;
% to take the function, a and b as input from the user
f = input('Enter the Function: ');
% to determine the bracket
a = 0;
b = 1;
% to replace the 0 of a with 1 for some cases when function has
% log10(x)
if ((f(a)==Inf) || (f(b)==Inf))

3
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid

a=1;
b=2;
end
while (((f(a))*(f(b)))>=0)
a = a+1;
b = b+1;
end
% Bisection Method
x_new = (a+b)/2;
fx = f(x_new);
while ((fx > 0.00001) || (fx < -0.00001))
fa = f(a);
fb = f(b);
if (fx < 0) && (fa <0)
a = x_new;
elseif (fx>0) && (fa>0)
a = x_new;
elseif (fx<0) && (fb<0)
b = x_new;
elseif (fx>0) && (fb>0)
b = x_new;
end
x_new = (a+b)/2;
fx = f(x_new);
end
% to print the root of the function
fprintf('\nRoot of the input function is = %f\n',x_new);

Output:
If we input
Enter the Function: @(x) x^3-x-1

Then

Root of the input function is = 1.324718

 False Positioning Method:


The method is described as the trial and error approach of using “false” or “test”
values for the variable and then altering the test value according to the result.

Consider an equation f(x) = 0, which contains only one variable, i.e. x.


To find the real root of the equation f(x) = 0, we consider a sufficiently small interval (a, b) where a < b such
that f(a) and f(b) will have opposite signs.
According to the False Position Method, this implies a root lies between a and b.
Also, the slope y = f(x) will meet the x-axis at a certain point between A[a, f(a)] and B[b, f(b)].
Now, the equation of the chord joining A[a, f(a)] and B[b, f(b)] is given by:

Let y = 0 be the point of intersection of the chord equation (given above) with the x-axis. Then,

4
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid

This can be simplified as:

Stoping Criteria is either f(x_new) ≈ 0 or we are provided to do a specified number of iterations or there is no
change in f(x_new).

Figure 9.2: Graph to Understand False Position Method

Matlab Code:
% False Position Method When Brackett is not given
clearvars;
clc;
% to take the function, a and b as input from the user
f = input('Enter the Function: ');
% to determine the bracket
a = 0;
b = 1;
% to replace the 0 of a with 1 for some cases when function has
% log10(x)
if ((f(a)==Inf) || (f(b)==Inf))
a=1;
b=2;
end
while (((f(a))*(f(b)))>=0)
a = a+1;
b = b+1;
end
% False Position Method
x_new = ((a*(f(b)))-(b*(f(a))))/((f(b))-(f(a)));
fx = f(x_new);
while ((fx > 0.00001) || (fx < -0.00001))
fa = f(a);

5
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid

fb = f(b);
if (fx < 0) && (fa <0)
a = x_new;
elseif (fx>0) && (fa>0)
a = x_new;
elseif (fx<0) && (fb<0)
b = x_new;
elseif (fx>0) && (fb>0)
b = x_new;
end
x_new = (a*(f(b))-b*(f(a)))/((f(b))-(f(a)));
fx = f(x_new);
end
% to print the root of the function
fprintf('\nRoot of the input function is = %f\n',x_new);

Output:
If we input
Enter the Function: @(x) 2*x-log10(x)-7
Then
Root of the input function is = 3.789276

You might also like