clear all; close all;

%Stationary Points Example

syms x

f = x^3 -50*x^2  - 100*x + 2;

df = diff(f); % 1st derivative

df2 = diff(df); %2nd derivative

% find Turning points
% need double() from convert symbolic variable

turn_pts_x = double(solve(df));

turn_pts_y = double(subs(f,turn_pts_x));

max_min = double(subs(df2,turn_pts_x));

%%%% Plot Function and maxima minima

ezplot(f,[-80 80]);

hold on;

% Check sign of max_min (df2 at turn_pts x)

[n m] = size(turn_pts_x);

for i=1:n
    if max_min(i) < 0 % Maxima
        plot(turn_pts_x(i),turn_pts_y(i),'o','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','b','MarkerSize',10);
        text(turn_pts_x(i),turn_pts_y(i)-50,'Max');
    else % Minima
        plot(turn_pts_x(i),turn_pts_y(i),'o','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','r','MarkerSize',10);
        text(turn_pts_x(i),turn_pts_y(i)-50,'Min');
    end
end

