You are on page 1of 4

LAB NUMBER 2

Bisection Method
Bisection Method (using For loop)
Definition: The bisection method in mathematics is a root-finding

method that repeatedly bisects an interval and then selects a subinterval in


which a root must lie for further processing.

Code for Bisection Method

clc;
close all;
%For closing all windows
clear all;
syms x;
%globally defined
f=input ('Enter the value of desired function=')
%Defining the Function
l=input('Enter the value of lower limit=')
%Taking lower limit
u=input('Enter the value of upper limit=')
%Taking upper limit
fl=subs(f,x,l);
%Substituting lower limit
in Function
fu=subs(f,x,u);
%Substituting upper
limit in Function
if (fl*fu>0)
%To check whether root
intersecting at x-axis
Display ('Error')
%Display the
"Error"message"
else
for (i=2:15)
%for loop for fifteen iterations
xm=(l+u)/2;
%Calculating mean
fxm=subs(f,x,xm);
%Substituting mean value in
Function
if (fxm*fl>0)
%condition of if
l=xm
%if condition is true then l will
be equal to xm
else if (fxm*fl<0)
%but if condition is false then
u=xm
%lower limit will be equal to
mean value
else if (fxm*fl==0)
%intersecting at x-axis is root
=u
Answer=xm
%Answer will be equal to mean
value
end
end
end

%ending if condition
%ending for loop
%ending else if condition

Root=xm
e(1)=0
e(i)=Root
Error=abs((e(i)-e(i-1))/e(i))
end
end

%Calculating Errors

Results :
Iterations

Upper Limit

Lower Limit
(l)

1
2
3
4
5
6
7
8
9
10

(u)
2
1.5
1.5
1.375
1.375
1.3438
1.3281
1.3281
1.3281
1.3262

1
1
1.25
1.25
1.3125
1.3125
1.3125
1.3203
1.3242
1.3242

Mean=l+u/
2
(Root)
1.5000
1.2500
1.3750
1.3125
1.3438
1.3281
1.3203
1.3242
1.3262
1.3252

11

1.3252

1.3242

1.3247

12

1.3252

1.3247

1.3250

13

1.3250

1.3247

1.3248

14

1.3248

1.3247

1.3248

15

1.3248

1.3247

1.3248

Error

1.0000
0.2000
0.9090
0.0476
0.0233
0.0118
0.0059
0.0029
0.0015
7.3692e004
3.6860e004
1.8426e004
9.2140e005
4.6072e005
4.6072e005
Root =

1.3248

LAB NUMBER 3

Bisection Method
Bisection Method (using While loop)
Task Number 1:Code for Bisection Method
clc;
close all;
clear all;
syms x;
f=input ('Enter the value of desired function=')
l=input('Enter the value of lower limit=')
u=input('Enter the value of upper limit=')
tol=input('Enter the value of tolerance')
fl=subs(f,x,l);
Function
fu=subs(f,x,u);
Function
if (fl*fu>0)
intersecting at x-axis or not
Display ('Error')
else
while(u-l>tol)
xm=(l+u)/2;
fxm=subs(f,x,xm);
Function
if (fxm*fl>0)
l=xm
be equal to xm
else if (fxm*fl<0)
u=xm
mean value
else if (fxm*fl==0)
=u
Answer=xm
value
end
end
end
Root=xm
end
end

%For closing all windows


%globally defined
%Defining the Function
%Taking lower limit
%Taking upper limit
%Taking tolerance

%Substituting lower limit in


%Substituting upper limit in
%To check whether root
%Display the "Error "message"
%using while loop
%Calculating mean
%Substituting mean value in
%condition of nested if
%if condition is true then l will
%otherwise
%upper limit will be equal to
%intersecting at x-axis is root
%Answer will be equal to mean
%ending loop
%Root will be equal to mean

Results of task number 1:


Tolerance=0.01
Iterations
1
2
3
4
5
6
7

Upper limit
(u)
2
1.5
1.5
1.375
1.375
1.3438
1.3281

Lower limit
(l)
1
1
1.25
1.25
1.3125
1.3125
1.3125

M=(u+l)/2
(Root)
1.5000
1.2500
1.3750
1.3125
1.3438
1.3281
1.3203
Root =

1.3203

Task Number 2: Write Difference between for and while loop with
respect to Bisection method?

Results of task number 2:


The main difference between for and while loop with respect to Bisection
method is that in for loop we defined(input) iterations in code but in while
loop we just give tolerance in our code according to which values are
displayed.

You might also like