Reputation: 135
How can I find the zeros, maxima, and minima of the polynomial x^3 - 10x + 20? So far, I have done the following
fhandle1 = @(x) x.^3 - 10.*x + 20;
figure(1);
hold on;
fplot(fhandle1,[0, 10]);
[xMin, yMin] = fminbnd(fhandle1, 2.5, 4);
plot(xMin, yMin, 'or')
[xzero,yzero] = fzero(fhandle1,5);
plot(xzero,yzero,'*g')
How can I find the maxima of the polynomial?
Upvotes: 1
Views: 572
Reputation: 30047
fminbnd
will return the max/min value of a function, which isn't necessarily the same as the maxima/minima.
The most complete way to approach this for any given polynomial leans on two facts
f(x)
occur when f'(x) = 0
f''(x)
You can use roots
to find all roots of a polynomial from its coefficients, and you can use polyder
to find the derivative coefficients for a polynomial.
Output
Code
coeffs = [1, 0, -10, 20]; % 1*x^3 + 0*x^2 - 10*x + 20
% Find zeros
r = realRoots( coeffs );
% Get the coeffs of the derivative
dcoeffs = polyder( coeffs );
% Minima/maxima are at roots of derivative
dr = realRoots( dcoeffs );
% Get coeffs of next derivative
ddcoeffs = polyder( dcoeffs );
% Minima: ddcoeffs > 0, Maxima: ddcoeffs < 0, Inflection pt: ddcoeffs = 0
minima = dr( polyval( ddcoeffs, dr ) > 0 );
maxima = dr( polyval( ddcoeffs, dr ) < 0 );
inflection = dr( polyval( ddcoeffs, dr ) == 0 );
% Plot (ranges would need to be based on extrema for a generic polynomial)
figure(1); clf; hold on; grid on;
x = -10:0.1:4;
plot( x, polyval( coeffs, x ), 'LineWidth', 1 ); % function line
plot( r, polyval( coeffs, r ), 'ok', 'LineWidth', 2 ); % roots
plot( minima, polyval( coeffs, minima ), 'vk', 'LineWidth', 2 ); % minima
plot( maxima, polyval( coeffs, maxima ), '^k', 'LineWidth', 2 ); % maxima
plot( inflection, polyval( coeffs, inflection ), 'xk', 'LineWidth', 2 ); % inflection
ylim( [-20, 50] );
% Helper function to return the real roots from polynomial coeffs
function r = realRoots( c )
r = roots( c ); % all roots
r = r( imag(r) == 0 ); % real roots
end
Upvotes: 3