1. 1. Bisection method
The MATLAB code is:
function xr= bis(xl, xu, ep)
fn=@(x) x^3-10*x^2+5
fl=fn(xl);
fu=fn(xu);
if(fl*fu>0)
fprintf('wrong guess!n');
xr=NaN;
return
end
err= abs(xl-xu)
iteration=0;
while(err>ep)
xr=(xl+xu)*0.5
fr=fn(xr)
if(fr*fu>=0)
%root lies between xr and xl
xu = xr
fr = fu
else
%root lies between xr and xu
xl= xr
fr=fl
end
err = abs(xl-xu)
iteration=iteration+1
fprintf('%f %f (%f,%f)n',xr,fn(xr),xl,xu);
%print x,f(x),(xl,xu)
End
Giving input as ‘bis(0.6,0.8,1e-5)’, The following results are obtained