coef=input('type the coefficients of the polynomial:');

while length(coef)<3||any(coef(1:max(1,length(coef)-2)))==0
    coef=input('type the coefficients of the polynomial:');
end
    
    p1=polyder(coef); %first derivative
    r=roots(p1); %roots of the first derivative

    findreal=find(imag(r)==0); %positions of real roots of the first derivative

    if isempty(findreal)
        disp('The are no real critical values')
    else
        realroots=r(findreal); % vector of the real roots
        disp('The  critical values are:')
        disp(realroots)
        
        p2=polyder(p1); % second derivative
               
        for k=1:length(realroots)
                   
            y=polyval(p2,realroots(k)); % the value of the second derivative in the jth critical value
            
            if y<0
                disp(['at x0=',num2str(realroots(k)),' the polynomial presents a local max']);
            elseif y>0
                disp(['at x0=',num2str(realroots(k)),' the polynomial presents a local min']);
            else
                disp(['at x0=',num2str(realroots(k)),' the test is inconclusive']);
            end
        end 
        
        %extra plot
        x=(min(realroots)-0.5):0.05:(max(realroots)+0.5);
        y1=polyval(coef,x);
        plot(x,y1)
    end





