Use the bisection method to find the roots of a polynomial o
Solution
Paste the below code in file name bisect.m file and run the program from command window.
function bisect()
syms a b c d x; % coefficients of polynomial ax^3+bx^2+cx+d
prompt = \'Enter a \';
a = input(prompt);
prompt = \'Enter b \';
b = input(prompt);
prompt = \'Enter c \';
c = input(prompt);
prompt = \'Enter d \';
d = input(prompt);
p = [a b c d];
y = poly2sym(p,x) % Generating polynomial from coefficients
x = -5:0.5:5; %x range -5<=x<=5
grid on;
ezplot(y,x);
title(\'Plot y = f(x)\')
xlabel(\'x values-->\')
ylabel(\'y values-->\')
%Implementation of Bisection method
k = input(\'Enter the first approximation xl(lower) \');
l = input(\'Enter the second approximation xu(upper) \');
acc = input(\'Enter the value of accuracy\');
fk = polyval(p,k);
fl = polyval(p,l);
%always enter lower root as negative and upper as positive
while((polyval(p,k) * polyval(p,l))>0)
k = input(\'Enter the first approximation xl(lower) \');
l = input(\'Enter the second approximation xu(upper) \');
end
while(abs(l-k)>acc)
m = (l+k)/2;
fk = polyval(p,k);
fm = polyval(p,m);
if(fk * fm < 0)
l = m;
else
k = m;
end
end
fprintf(\'The root of the equation is %f\',m);
end

