Root by Bisection Method

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

FINDING THE ROOT BY BISECTION METHOD:

clc
f=@(x) x^2-2*x-2;
%f=@(x) x^2-7;
%f=@(x) x^3-4*x-9;
a=input('enter a value:');
b=a+1;
x=1;
while(x<100)
if(f(a)*f(b)<0)
fprintf('roots are between a=%0.4f and b=%0.4f\n',a,b)
fprintf('f(a)=%0.4f and f(b)=%0.4f\n',f(a),f(b))
break
else
fprintf('there are no roots between a=%0.4f & b=%0.4f\n',a,b)
fprintf('f(a)=%0.4f and f(b)=%0.4f\n',f(a),f(b))
a=b;
b=b+1;
end
x=x+1;
end
z=1;
fprintf('a b G f(a) f(b) f(G)\n')
while(z<100)
G=(a+b)/2;
fprintf('%0.4f %0.4f %0.4f %0.4f %0.4f %0.4f\n',a,b,G,f(a),f(b),f(G))
if(f(G)*f(a)<0)
b=G;
else
a=G;
end
if(abs(f(G))<0.0001)
fprintf('root of given equation is=%0.4f\n',G)
break
end
z=z+1;
end

You might also like