Accelerating the pace of engineering and science

Symbolic Math Toolbox

Using Derivatives to Find Maxima, Minima, and Inflection Points

This demonstration shows how to find first and second derivatives using Symbolic Math Toolbox.

First Derivatives: Finding Local Minima and Maxima

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:

f := (3*x^3 + 17*x^2 + 6*x + 1)/(2*x^3 - x + 3)

(3*x^3 + 17*x^2 + 6*x + 1)/(2*x^3 - x + 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:


MuPAD graphics

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:

assume(x in R_):

To find a horizontal asymptote, compute the limit of f for x approaching positive and negative infinities. The horizontal asymptote is x = 3/2:

limit(f, x = infinity), limit(f, x = -infinity);

3/2, 3/2

To find a vertical asymptote of f, find the roots of the polynomial expression that represents the denominator of f:

solve(2*x^3 - x + 3 = 0, x);

R_ intersect RootOf(z^3 - z/2 + 3/2, z)

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, MaxDegree=2. Increasing this value, you can get explicit solutions for higher-order polynomials. For example, specifying MaxDegree=3 results in an explicit solution:

solve(2*x^3 - x + 3 = 0, x, MaxDegree = 3);

{- 1/(6*(3/4 - (241^(1/2)*432^(1/2))/432)^(1/3)) - (3/4 - (241^(1/2)*432^(1/2))/432)^(1/3)}

You can approximate the exact solution numerically by using the float function. To access the result of the previous evaluation, use the last function or its shortcut %:



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 diff function:

g := diff(f, x)

(9*x^2 + 34*x + 6)/(2*x^3 - x + 3) - ((6*x^2 - 1)*(3*x^3 + 17*x^2 + 6*x + 1))/(2*x^3 - x + 3)^2

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 float function:

solve(g = 0, x,  MaxDegree = 4):

{-0.1892448205, 1.285976467}

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.

Second Derivatives: Finding Inflection Points

Computing the second derivative lets you find inflection points of the expression. The most efficient way to compute second or higher-order derivatives is to use the sequence operator $:

h := diff(f, x $ 2)

(18*x + 34)/(2*x^3 - x + 3) - (2*(6*x^2 - 1)*(9*x^2 + 34*x + 6))/(2*x^3 - x + 3)^2 - (12*x*(3*x^3 + 17*x^2 + 6*x + 1))/(2*x^3 - x + 3)^2 + (2*(6*x^2 - 1)^2*(3*x^3 + 17*x^2 + 6*x + 1))/(2*x^3 - x + 3)^3

To find inflection points of f, solve the equation h = 0. For this equation the symbolic solver returns complicated result even if you use the MaxDegree option:

solve(h = 0, x, MaxDegree = 4)

R_ intersect RootOf(z^6 + (45*z^5)/34 + (9*z^4)/34 - (699*z^3)/68 - (249*z^2)/68 + (63*z)/68 + 43/17, z)

To get the simpler numerical result, solve the equation numerically by using the numeric::solve function. Use the option AllRealRoots to obtain all real solutions of the expression:

numeric::solve(h = 0, x, AllRealRoots)

{0.5787184257, 1.865154363}

The expression f has two inflection points: x = 0.579 and x = 1.865.