Differentiate Functions

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

To compute derivatives of functions, use the differential operator D. This operator differentiates both standard mathematical functions and your own functions created in MuPAD®. For example, find the first derivatives of the following standard mathematical functions implemented in MuPAD:

D(sin), D(exp), D(cosh), D(sqrt), D(heaviside)

Create your own function with one variable and compute a derivative of this function:

f := x -> x^3:

Alternatively, use ' as a shortcut for the differential operator D:

f := x -> sin(x)/x^2:

Computing the first derivatives of a function lets you find its local extrema (minima and maxima). For example, create this function and plot it on the interval -10 < x < 10:

F := x -> x^3*sin(x);
plot(F, x = -10..10)

Find the local extrema of F on the interval -10 < x < 10. If the point is a local extremum (either minimum or maximum), the first derivative of the function at that point equals 0. Therefore, to find the local extrema of F, solve the equation F'(x) = 0. Use the AllRealRoots option to return more than one solution.

extrema := numeric::solve(F'(x) = 0, x = -10..10, AllRealRoots)

Now, compute the corresponding values of F. For example, compute F for the third element, -2.455643863, in the solution set:


To compute the values of F for all local minima and maxima, use the following command. Here, $ is used to evaluate F for every element of the extrema set.

points := {[x, F(x)] $ x in extrema}

Plot function F with extrema points:

plot(F, points, x = -10..10)

To compute a derivative of a multivariable function, specify the differentiation variable. The operator D does not accept the variable names. Instead of providing a variable name, provide its index. For example, integrate the following function with respect to its first variable x. Then integrate the function with respect to its second variable y:

f := (x, y) -> x^2 + y^3:
D([1], f);
D([2], f)

The list of indices accepted by the operator D refers to the order in which you provided the variables when creating a function:

f := (x, y) -> x^2 + y^3:
D([1], f);
f := (y, x) -> x^2 + y^3:
D([1], f)

To find second- and higher-order partial derivatives of a function, use the same index two or more times. For example, compute the second-order partial derivatives with respect to x and with respect to y:

f := (x, y) -> x^3*sin(y):
D([1, 1], f);
D([2, 2], f)

To compute second- and higher-order derivatives with respect to several variables (mixed derivatives), provide a list of indices of differentiation variables:

f := (x, y) -> x^3*sin(y):
D([1, 2], f);


To improve performance, MuPAD assumes that all mixed derivatives commute. For example, . This assumption suffices for most engineering and scientific problems.