butiran

m func extreme points min max

a quadratic function

plot code

finding root

finding extreme points

other case

% function_extreme_points_min

% clear previous states
clear;

% define range of x
xbeg = 0;
xend = 10;

% define a polynomial via its coefficients
a = [-1 12 -32];

% create data for ploting (x, px)
x = [];
px = [];
for i = xbeg:xend
    x = [x i];
    px = [px polyval(a, i)];
end

% calculate derivative
b = polyder(a);

% create data for ploting (x, qx)
x = [];
qx = [];
for i = xbeg:xend
    x = [x i];
   qx = [qx polyval(b, i)];
end

% plot functions px and qx
plot(x, px, 'rs-', x, qx, 'bo-');
xlim([xbeg xend]);
legend('p(x)', 'q(x)', ...
    'location', 'northeast');
xlabel('x');
ylabel('y');
grid on;
%{%}

% create symbolic function p(x)
ps = poly2sym(a);

% create function from string
fpx = inline(ps);

% find roots
root1 = fzero(fpx, 0);
root2 = fzero(fpx, 10);

% show results
fprintf("p(x) = %s\n", ps);
fprintf("root-1 = %.2f\n", root1);
fprintf("root-2 = %.2f\n", root2);
%}

% create symbolic function for q(x)
qs = poly2sym(b);

% create numerical function from string
fqx = inline(qs);

% find root
extreme = fzero(fqx, 0);

% get next derivative
c = polyder(b);

% create symbolic functio for r(x)
rs = poly2sym(c);

% check value of extreme derivative
ved = polyval(c, extreme);
fprintf('Extreme is at x = %.2f\n', extreme);

% show analysis
fprintf(['Second derivative value on extreme' ...
    ' = %.2f.\n'], ved);
if ved > 0
    fprintf('It is > 0, so it is a minimum point.')
elseif ved < 0
    fprintf('It is < 0, so it is a maximum point.')
else
    fprintf('It is = 0, so it is an inflection point.')
end