clc
clear all

n = input('Enter the degree of the polynomial: '); %The user is prompted to input the required degree for the polynomial

for i = 0:n %initial iteration for the FOR loop is zero
    c(i+1) = input(['Enter the coefficient of x^' num2str(i) ': ']); %The user is prompted to input the coefficient(c) multiplying x^0.                                                                   
                                          % The loop then runs using increasing values of i to represent the power of x until the coefficients  
                                          % of all x values in the equation
                                          % have been inputted.
end
x=[-100:100]; %The parameters of the x values for the graph are set here.
P = flip(c); %The result of the previous FOR loop returns a vector which must be flipped in order to be used to plot a graph.
ps = 0;%beginning y value 
k = 0;%power of each part of the polynomial
for i = 0:n %Another FOR loop is used to determine the values of x and y to plot a graph with.
    k = n - i; %k represents the power of x. The first power is equal to the degree and with each iteration it decreases by one.
    y = P(i+1)*x.^k; %y is determined by multiplying each value from the vector P by each x value with their respective powers.
    ps = y +ps; %ps is equal to the previous value of ps plus the calculated y value.
end

plot(x,ps) %The graph of x against ps is plotted to show the graph of the polynomial inputted by the user.
title('\color{blue} Graph of Polynomial','fontweight','bold','fontsize',16)%Setting the title of the graph to the colour blue and bold with a size 16 font.
xlabel('\color{red} X-Axis','fontweight','bold','fontsize',12)%setting the axis titles to red with bold text at size 12.
ylabel('\color{red} Y-Axis','fontweight','bold','fontsize',12)
grid on %The grid lines are made visible in the plot area.
grid minor %The minor grid lines are also made visible.
xline(0); %the line of x=0 is plotted
yline(0); %the line of y=0 is plotted

if ps == 0
    r = find(x)
    disp('The roots of this polynomial are: ',num2str(r))
    
    else
        disp('This polynomial does not have any roots within the range of values')   



end


      
