%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
infobox = msgbox(["Polynomials need to be degree 0 or more in order to be formed"; "use degree = -10 for a break condition"], "Info Box ");%messagebox displaying break condition for while loop
pause(5)%give time to the reader to understand the message
while true%creates while loop for ease of use instead of having to press run after every polynomial entered
    clc% clears the command window
    clear%clears all variables after every graph plot to ensure no overwriting issues
    degree = input("What is your desired degree of polynomial?..."); % assigns the degree of chosen polynomial to a variable to be used later
    if degree <= -11 || degree >= -9 && degree < 0%sets if statement for if the degree of the polynomial is less than 0, as it cant be formed and the user must enter a different degree
        messbox1 = msgbox("You need to input a degree of 0 or more in order for a polynomial to be formed", "Sort it out");%makes the user realise that a poly couldnt be formed due to the degree they entered
        pause
    end
    if degree >=0%is a polynomial can be formed...
        for i = 0:(degree)%creates a for loop in order to collect the different multipliers/coefficients for the degrees of x
            poly = input(['Please enter value for coeffieicent multiplying x^' num2str(i) '...']);%asks for input
            H(:, i+1) = poly;%adds that input into the next line of array H
        end
        coef = fliplr(H);%flips the array to get the coeffiencts in the right order for plotting and finding the zero of that polynomial
        px = [-100:0.0001:100];%px is the range of x values used to enter into a formula to get the coresponding y values. The more elemts within this vector, the better the accuracy
        py = 0;%initialises the sum 
        for i = 0:1:(degree)
            exp = degree - i;%the exponent of the "X" variable
            p = coef(i+1)*px.^exp;%the formula used to generate the polynomial and enter in the range of x values stored in px(), 
            py = py + p;%this adds all the different components of the polynomial to get the final Y value corresponding to each X value within the array px and stores these in an array ps
        end
        bisect = sign(py);%creates a logical array of values to find sign changes within the y values
        locate_root = find([0 diff(bisect)]~=0);%locates the roots of the sign changes
        root_s = px(locate_root);%finds the corresponding x values to the position of the sign changes in y
        roots_sort = sort(root_s);%sorts these x roots into ascending order
        num_of_root = numel(root_s);%number of roots
        if num_of_root > 0%if there are real roots, then execute the following...
            disp("There are")
            disp(num_of_root)
            disp("Real roots that lie in the interval [-100:100].")
            disp("They are x = ")
            disp(roots_sort)
            minlim = min(root_s)-2;%declares the max and min limits of the x axis in order for the graph to look better
            maxlim = max(root_s)+2;
            plot(px, py);%plots graph of the x values against the y values
            xlim([minlim maxlim])%sets x limits
            ylim([-20 20])%sets y limits
            title(['Graph of polynomial of order x^ ' num2str(degree)], 'FontSize',16,'FontWeight','bold','Color','blue')%change title
            xlabel('X Axis', 'FontSize',12,'FontWeight','bold','Color','r') %axis label x
            ylabel('Y Axis', 'FontSize',12,'FontWeight','bold','Color','r')%axis label y
            yline(0,'-r', "X-Axis");%shows a standard line at y = 0/x axis to show the intersection between the polynomial line and the x axis
            xline(0, '-r', 'Y-Axis');%shows the Y axis
            grid minor %sets a local minor grid line
            grid on%sets the grid on
            disp("Press any key to restart")
            pause
        else%if there is no real roots, execute the following
            disp("There are no real roots that lie between x = [-100:100]")
            plot(px, py);%plots graph
            title(['Graph of polynomial of order x^ ' num2str(degree)], 'FontSize',16,'FontWeight','bold','Color','blue')%change title
            xlabel('X Axis', 'FontSize',12,'FontWeight','bold','Color','r') %axis label
            ylabel('Y Axis', 'FontSize',12,'FontWeight','bold','Color','r')%axis label
            ylim([-20 20])%sets y limit
            yline(0,'-r', "X-Axis");
            grid minor %sets a local minor grid line
            grid on%sets the grid on
            
            disp("Press any key to restart")
            pause
        end 
        
    end
    if degree == -10%sets a break condition to end the while loop
        messbox2 = msgbox("Exiting Program...", "Exiting");
        pause(4)
        return
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Dan Morris

         

    

    






