Lecture No. 2
Lecture No. 2
𝑥1
𝑥2
Algorithm for Bisection
Method
• Initial Condition: The function f(x) must be continuous in [,b]
and f(a) and f(b) must have opposite signs.
Step1:Find the midpoint of the interval: c= (a+b)/2.
Step2:Evaluate f(c):
Step3:If f(c) =0, c is the root.
• If f(a)⋅f(c)< 0, the root lies in [a,c] update b=c
• If f (b) ⋅f(c) <0, the root lies in [c,b]; update a=c.
Step4:Repeat until the interval is sufficiently small or a desired
accuracy is achieved.
Advantages
• Simple and easy to implement.
• Always convergent
• The root bracket gets halved with each iteration -
guaranteed.
Disadvantages
Slow convergence
If one of the initial guesses is close to the root, the
convergence is slower
If a function f(x) is such that it just touches the x-axis it
will be unable to find the lower and upper guesses.
Function changes sign but root does not exist
Example 1
Find a root of an equation f(x)=x^3-x-1 using Bisection method
Solution
Since
f(x)=x^3-x-1
x=0 then f(0)=-1 <0
x=1 then f(1)=-1 <0
x=2 then f(2)=5 >0
So root lies between x= 1 and x=2
x 0 1 2
f(x) -1 -1 5
Example 1(Cont.)
• 1st iteration :
x0=(1+2)/2=1.5
f(x0)=f(1.5)=0.875>0
2nd iteration :
x1=(1+1.5)/2=1.25
f(x1)=f(1.25)=-0.29688<0
Example 1(Cont.)
3rd iteration :
x2=(1.25+1.5)/2=1.375
f(x2)=f(1.375)=0.22461>0
4th iteration :
x3=(1.25+1.375)2=1.3125
f(x3)=f(1.3125)=-0.05151<0
Table
n a f(a) b f(b) c=(a+b)2 f(c) Update
1 1 -1 2 5 1.5 0.875 b=c
2 1 -1 1.5 0.875 1.25 -0.29688 a=c
3 1.25 -0.29688 1.5 0.875 1.375 0.22461 b=c
4 1.25 -0.29688 1.375 0.22461 1.3125 -0.05151 a=c
5 1.3125 -0.05151 1.375 0.22461 1.34375 0.08261 b=c
6 1.3125 -0.05151 1.34375 0.08261 1.32812 0.01458 b=c
7 1.3125 -0.05151 1.32812 0.01458 1.32031 -0.01871 a=c
8 1.32031 -0.01871 1.32812 0.01458 1.32422 -0.00213 a=c
9 1.32422 -0.00213 1.32812 0.01458 1.32617 0.00621 b=c
10 1.32422 -0.00213 1.32617 0.00621 1.3252 0.00204 b=c
11 1.32422 -0.00213 1.3252 0.00204 1.32471 -0.00005 a=c
Questions
• Find a root of the following
equation using Bisection method
i) f(x)=x^2 – sin(x) – 0.5
ii) f(x)=2x^3-2x-5
iii) f(x)= cos(x) – x *e^x
iv) f(x)=3x + sin(x) – e^x
Matlab code
• clc
• f=@(x)x^2-sin(x)-0.5 %main function
• a=0; % value of x1
• b=2; %value of x2
• c=(a+b)/2 %calculate x3
• for i=1:15
• iteration = i+1
• if f(a)*f(b)<0
• if f(a)<0 && f(c)<0
• a=c;
• else
• b=c;
• end
• end
• c=(a+b)/2
• end