% Bisection Method
clc,clear
f=@(x) -0.6*x^2+2.4*x+5.5;
xl=5; xu=10; % lower and upper values
ezplot(f,[xl,xu]); % Graph of function
ea=100; % Approximate percent relative error
es=11; % Prespecified percent tolerance
c=1; % Count of steps( Number of iterations)
xrp=0; % xrp-previous
while ea > es
xr=(xl+xu)/2;
ea=abs((xr-xrp)/xr)*100;
Result(c,:)=[ c , xl ,xu , xr , f(xl), f(xu), f(xr), ea ];
if f(xl)*f(xr)<0
xu=xr;
elseif f(xl)*f(xr)>0
xl=xr;
else
disp(['The root xr=' xr])
break;
end
c=c+1;
xrp=xr; % To change xrp-previous with xr-present
end
Result
--------------------------------------------------------------
>>
Result =
𝒄 𝒙𝐥 𝒙𝐮 𝒙𝐫 𝒇(𝒙𝐥 ) 𝒇(𝒙𝐮 ) 𝒇(𝒙𝐫 ) 𝜺𝒂
1.0000 5.0000 10.0000 7.5000 2.5000 -30.5000 -10.2500 100.0000
2.0000 5.0000 7.5000 6.2500 2.5000 -10.2500 -2.9375 20.0000
3.0000 5.0000 6.2500 5.6250 2.5000 -2.9375 0.0156 11.1111
4.0000 5.6250 6.2500 5.9375 0.0156 -2.9375 -1.4023 5.2632
Bewar Y. Ali Numerical Analysis