## Symbolic Math Toolbox |

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

- First Derivatives: Finding Local Minimum and Maximum of the Function
- Second Derivatives: Finding inflection points of the Function

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)

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:

plot(f)

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);

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);

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);

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 %:

float(%)

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)

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):

float(%)

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.

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)

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)

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)

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