We have given the following function;
Look at to the plot of the function to learn where the function crosses the x-axis. MATLAB can do it for us:
>> fx = inline ( ' 3 *x + sin ( x) - exp ( x) ')
>> fplot ( fx, [ 0 2 ]) ; grid on
An algorithm for Halving the Interval (Bisection):
The MATLAB program for this algorithm is given.
function rtn=bisec(fx,xa,xb,n)
%bisec does n bisections to approximate
% a root of fx
x=xa; fa=eval(fx);
x=xb; fb=eval(fx);
for i=1:n;
xc=(xa+xb)/2; x=xc; fc=eval(fx);
X=[i,xa,xb,xc,fc];
disp(X);
if fc*fa<0
xb=xc;
else xa=xc;
end
end
save with the name bisec.m. Then;
>> fx=' 3 *x + sin ( x) - exp ( x) '
>> bisec(fx,0,1,13)
Modify this MATLAB program for the bisection method for using a tolerance value of 1E-4.