clc % Cleans the command window
clear all % Cleans the workspace 
close all % Deletes all figures whose handles are not hidden

D=input('Enter the degree of the polynomial: '); % Asks the user to enter the degree of the polynomial and assign the number to D 

for i=1:D+1 % The input is getting looped according to the degree of polynomial + 1, 1 is being added as the starting value could not be set as 0
    C(i)=input(['Input value of coefficient multiplying x^' num2str(i-1) ': ']); % Asks the user to input a value for the coefficient
end % Terminates the for function

 X=[-100:0.1:100]; % To create a range of values between -100 and 100 with a step of 0.1, a low step is used to make the graph as smooth as possible, a semi colon is used at the end so the code doesnt get displayed in the command window
 F=0; % I assinged the starting value as 0, as it requires a number to be assigned to the letter F
 
for i=0:D % for is used to repeat equation depending on the degree of the polynomial
 F=F+C(i+1)*X.^i; % This is the equation for calcualting the polynomial
end

S=sign(F); % The sign function returns an array to S the same size as F, if the element S is greater than F the value 1 is returned, if S is smaller than F a value of -1 and if S and F are the same size a value of 0 is returned 
K=1; % I set the K values at 1 as calculation later on would no be able to work without a number assigend to the K 
K2=1;
R=0; % I set the R values to 0 as they where i want the calculations to start of at
R2=0;

for i=1:1:length(S)-1 % I used the length of S -1 for the amount of times the section needs to be looped, a values of 1 is taken away as i want the loop to start at a value 0
  if S(i)==0 % The if evaltues the expression next to it, in this situation it looks to see if the value of S is 0, if the value is 0 it will keep doing the untill it reachs the elseif where it will then skip to the end
      E(K2)=i; % E represents the exact value of when the S is equal to 0
      K2=K2+1; % 1 is added in the section to count how many time the graph would cross the x axis at 0
   elseif S(i)~=S(i+1) && (abs(S(i))==1 && abs(S(i+1))==1) % For this elseif is used && which how 'and' is represented in the code, the code is for finding where the  S value changes as this point indicates a root
      L(K)=i; % L represents the last number before the S changes
      H(K)=i+1; % H represents the first number when the S changes
      K=K+1; % 1 is added to K to count how many times the S changes value between 1 and -1
  end
end 

if K>1 % This if statement will only work if the K value is greater than 1 
    for J=1:1:K-1 % This for loop is used to do the equation below equal to the amount of times the K value changes, i minus 1 from the value due to me starting it of already at 1
       R(J)=X(L(J))+((X(H(J))+X(L(J)))/2); % This equation is used to calculate a number between the H and L, as can be one of the values of the root of the polynomial. The number produced from this equation will be saved in R which represents the root values
    end
  elseif K2>1 % The if statement will only work if the K2 is also greater than 1
         R2=X(E); % This saves the exact value into R2 which represents the value of the root
end
  
a=[R2 R]; % This puts all the values for the roots into an array
s=sort(a); % sort is used to rearrange all the values in array into an acsending order 

N=K+K2-2; % I added the two counts together to get the number of roots, i then -2 from the value as the numbers both started of with a value of 1 as this meant the number was inflated  

if     N==0 % Using == means the if statement will only work if the number of roots is equal to 0
       disp('The number of real roots is equal to 0') % When the number of roots this statements will be displayed in the Command Window
elseif N>=1 % I used if N>=1 as i wanted this to only work when the number of roots is greater than 0 
       disp('The number of real roots is equal to :') % The disp function displays what is located in the brackets after it, if its text in apostrophes it will put what is in the apostrophes into the command window
       disp(N) % As there is no apostrophes the value allocated to the letter will be displayed instead
       disp('The roots are :')
       disp(s) 
end

M=max(s); % Max is used to find the biggest value in the sort
m=min(s); % Min is used to find the smallest value in the sort

plot(X,F,'color',[0 0 1]) % This will plot the polynomial on the y axis aganist the range of X values on the x axis, using colour and using the RGB the graph is plotted in blue
title('\fontsize{16}A graph displaying the entered polynomial','color',[0 0 1],'fontweight','bold') % Title displays a title on the graph, using apostrophes i can edit how the title looks such as the colour, size and boldness
xlabel('\fontsize{12}x-axis','color',[1 0 0],'fontweight','bold') % xlabel lets me label the x axis and i can also edit the way the text looks
ylabel('\fontsize{12}y-axis','color',[1 0 0],'fontweight','bold') % ylabel lets me label the y axis and i can also edit the way the text looks
grid minor % grid minor displays grind the background behind the graph
hold on % i use hold on to delay the plot of the graph until the if statement has been completed

if N==0 
   xlim([-10,10]) % xlim limits the size of the x axis shown to the user
   ylim([-30,50]) % ylim limits the size of the y axis shown to the user
elseif N~=0 % ~= is used so the if statement works when the value of N is not equal to 0
         xlim([m-2,M+2]) % I use M and m which are the min and max values of the roots, i then add 2 to the max and minus 2 from the min so that more of the graph can be shown without being cut off
         ylim([m-2*10,M+2*10]) % I used the same M and m values for the y axis but i multipled the values by 10 as the y axis is much larger than the x axis
         xticks([m-2:1:M+2]) % xticks with a step of 1 means that from the min to the max value on the x axis the value will be indicated when the x changes by 1
end

         
    
    
   


