This demonstration shows how to find extrema of functions using analytical and numerical techniques using the Symbolic Math Toolbox.
First Derivatives: Finding Local Minimum and Maximum of the Function
Second Derivatives: Finding Inflation Points of the Function
Limits : Functions with Suprema
Computing the first derivative of an expression helps you find local minima and maxima of that expression. For example, create a rational expression where the numerator and the denominator are polynomial expressions:
syms x f = (3 * x^3 + 17 * x^2 + 6 * x + 1)/(2 * x^3 + x * -1 + 3)
Plotting this expression shows that it has horizontal and vertical asymptotes, a local minimum between -1 and 0, and a local maximum between 1 and 2:
By default, when you operate on this expression, results can include both real and imaginary numbers. If you are interested in real numbers only, you can set the permanent assumption that x belongs to the set of real numbers. This allows you to avoid complex numbers in the solutions and it also can improve performance:
To find a horizontal asymptote, compute the limit of for x approaching positive and negative infinities. The horizontal asymptote is :
[limit(f, x, sym(inf)), limit(f, x, -sym(inf))]
To find a vertical asymptote of , find the roots of the polynomial expression that represents the denominator of :
solve(2 * x^3 + x * -1 + 3 == sym(0), x)
To get an explicit solution for such equations, try calling the solver with the option
MaxDegree . The option specifies the maximum degree of polynomials for which the solver tries to return explicit solutions. By default,
= 2 . Increasing this value, you can get explicit solutions for higher-order polynomials. For example, specifying
= 3 results in an explicit solution:
solve(2 * x^3 + x * -1 + 3 == 0, x, 'MaxDegree', 3)
You can approximate the exact solution numerically by using the
Now find the local minimum and maximum of the expression
f . If the point is a local extremum (either minimum or maximum), the first derivative of the expression at that point is equal to zero. To compute the derivative of an expression, use the
g = diff(f, x)
To find the local extrema of
f , solve the equation
g = 0 . If you use the
MaxDegree option, the solver returns the long explicit solution, which can be approximated by using the
solve(g == 0, x, 'MaxDegree', 4); extrema = vpa(ans, 6)
The plot of the expression
f shows that x = -0.189 is a local minimum of the expression, and x = 1.286 is its local maximum.
fplot(f) hold on plot(extrema, subs(f,extrema), '*') hold off
Computing the second derivative lets you find inflection points of the expression.
h(x) = simplify(diff(f, x, 2))
To find inflection points of , solve the equation
h = 0 . For this equation the symbolic solver returns a complicated result even if you use the
solve(h == 0, x, 'MaxDegree', 4)
To get the simpler numerical result, solve the equation numerically by using
vpasolve; specify the search range to restrict the returned results to all real solutions of the expression:
inflection = vpasolve(h == 0, x, [-inf, inf])
f has two inflation points: x = 0.579 and x = 1.865.
fplot(f) hold on plot(extrema, subs(f,extrema), '*') plot(inflection, subs(f,inflection), '*') hold off
Not all functions can be treated analytically; the function
is very flat at the origin and it oscillates infinitely often near , becomes linear as it approaches zero and oscillates again near .
f = @(x) tan(sin(x))-sin(tan(x))
f = function_handle with value: @(x)tan(sin(x))-sin(tan(x))
fplot(f, [-pi , pi])
Most important for our purposes here, fplot has picked the limit on the y-axes to be
ans = 1×2 -2.5488 2.5572
What is happening at ?
MATLAB uses double precision arithmetic, so evaluates to one of the oscillations.
ans = 1.5708
ans = 2.5161
The Symbolic Math Toolbox uses exact arithmetic, which shows the function is undefined.
syms x sym(pi/2)
F = sym(f)
We can also try to look at the value with a Taylor Series.
T = taylor(F,x,'Order',10,'ExpansionPoint',0)
hold on fplot(T) ylim ([-3 3]) hold off
We learn in calculus that a maximum occurs at a zero of the derivative. But this function is not differentiable in the vicinity of . We can analytically differentiate using the Symbolic Math Toolbox.
fplot(diff(F), [-pi , pi])
We can sample the function N times near to get a numerical approximation to the value of the maximum. Is that good enough?
N = 100; xValues = 3*pi/8 + pi/4*rand(1,N)
xValues = 1×100 1.8180 1.8895 1.2778 1.8955 1.6748 1.2547 1.3968 1.6076 1.9301 1.9359 1.3019 1.9404 1.9299 1.5593 1.8066 1.2895 1.5093 1.8973 1.8003 1.9317 1.6931 1.2061 1.8450 1.9117 1.7112 1.7732 1.7618 1.4862 1.6929 1.3125 1.7326 1.2031 1.3956 1.2144 1.2544 1.8248 1.7238 1.4271 1.9244 1.2052 1.5227 1.4778 1.7793 1.8026 1.3249 1.5628 1.5281 1.6857 1.7352 1.7708
ySoln = f(xValues)
ySoln = 1×100 0.7260 1.5080 1.5932 1.5614 1.3796 1.3158 2.0658 2.4586 1.8194 1.8541 1.9088 1.8793 1.8178 2.3439 0.6145 1.7447 2.0697 1.5775 0.5629 1.8290 2.4930 0.8543 1.0347 1.6931 2.2371 0.5024 0.6011 2.2489 2.4891 2.0499 1.3564 0.8308 2.0986 0.9208 1.3122 0.8011 1.7177 0.9333 1.7830 0.8466 0.6138 2.5047 0.4857 0.5809 2.2051 2.5133 2.5387 2.2247 1.2583 0.5153
ans = 2.5387
Determine the maximum from a mathematical proof.
so and which means consequently
As , oscillates and blows up; so is actually not defined at all at this point as was shown above
Now we can take a look at the numerical value.
format long 1 + tan(1)
ans = 2.557407724654902