%clears previous code
clc
clear all
close all

%asks user for degree and coefficient multipliers
deg=input('enter degrees of the polynomial:');
i=0;
for i=1:deg+1
    coef(i)=input(['enter coefficient multiplying x^' num2str(i-1) ': ']);
end

%generates x values
x = [-100:0.1:100];

%calculates the polynomial values
P=0;
for i=0:deg
    P =P+ coef(i+1).*x.^i;
end

%determines if there are any roots
S=sign(P);
k=1;
k2=1;
for i=1:1:length(S)-1
    if S(i)==0
        e(k2)=i;
        k2=k2+1;
    elseif S(i)~=S(i+1) && (abs(S(i)))==1 && (abs(S(i+1)))==1
        L(k)=i;
        H(k)=i+1;
        k=k+1;
    end
end

%determines root locations
R=0;
R2=0;
if k>1
    for j=1:1:k-1
        R(j)=x(L(j))+((x(H(j))+x(L(j)))/2);
    end
elseif k2>1
        R2=x(e);
end

%moves roots to a single array
A=[R R2];

%displays whether or not there are any roots and displays the amount
kt=k+k2-2;
if kt>0
    disp('there are ' )
    disp(kt)
    disp('roots')
elseif kt==0
    disp('there are no roots')
end

%sorts roots in acsending order and finds the minimum/maxium root values
B=sort(A);
M=min(B);
m=max(B);

%displays the roots if there are any
if kt>0
    disp('the roots are')
    disp(B)
end

%plots the x values against the polynomial values on a graph
plot(x,P,'color',[0 0 1])
%limits the x axis of the graph
xlim([M-2,m+2])
xticks([M-2:1:m+2]);

%changes the astetics of the graph
title('Graph of polynomial','fontsize',16,'fontweight','bold','color','b')
xlabel('x-label','fontsize',12,'fontweight','bold','color','r')
ylabel('y-label','fontsize',12,'fontweight','bold','color','r')

%creates major and minor grid lines on the graph
grid minor
