clc
clear all
%% Degree polynomial
x = input('Enter degree of your polynomial:');
y = zeros(1,x+1);
%% Coefficient Values
for i = 1:x+1
    f(i) = input('Input value of coefficient multiplyng x^():');
    y(1,x+2-i) = f(i);
end
%% Plot Function 
x = linspace(-100,100,2000); % This means 2000values generated between -100 and 100
yvals = polyval(y,x); %polyval evaluates the polynomial p at each point in x
r = roots(y);
tf = isreal(r);

  if tf<=0
     disp('No real roots to be found in considered interval [-100,100]')
     
  else tf>=0;
     disp('It exist rl roots in considered interval [-100,100]')
    
  end
  
r1 = sort(r) %roots value sorted in ascending orders
plot(x,yvals,'b')
%% Edit Graph
title ('Graph of Polynomial','FontSize',16,'fontWeight','bold','Color','b');
ylabel('y-axis','FontSize',12,'fontWeight','bold','Color','r');
xlabel('x-axis','FontSize',12,'fontWeight','bold','Color','r');
ylim('auto')
xlim('auto')
grid on
grid minor
%% Notes
% ylim([-10 10])
% xlim([-10 10])

%  return
%  Bisection Method (I learned this method from an exercise in a tutorial, 
%  but I don't know how to get "vector P the indices for which
%  P (x) = 0 and the indices at which P (x) sign changes".(just need a and b)
%  xn=a
%  xp=b
%  xmp=(xp+xn)/2;
%  ymp =polyval(y,xmp);
%  for iteration = 1:30
%     if ymp < 0
%         % ymp is less than zero
%         xn=xmp;
%     else
%         % ymp is greater than zero
%         xp=xmp;
%     end
%     xmp = (xn + xp) / 2;
%     ymp = polyval(y,xmp);
% end
% xmp
% ymp              
% ylim([2-ymp 2+ymp])
% plot(xmp,ymp,'r.','MarkerSize',30)

