Documentation Center

  • Trial Software
  • Product Updates

diff

Differentiate an expression or a polynomial

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

Syntax

diff(f)
diff(f, x)
diff(f, x1, x2, …)

Description

diff(f, x) computes the derivative of the function f with respect to the variable x.

diff(f, x) computes the partial derivative of the arithmetical expression (or polynomial) f with respect to the indeterminate x.

diff(f) computes the 0th derivative of f. Since the 0th derivative of f is f itself, diff(f) returns its evaluated argument.

diff(f, x1, x2, ...) is equivalent to diff(...diff(diff(f, x1), x2)...). In both cases, MuPAD® first differentiates f with respect to x1, then differentiates the result with respect to x2, and so on. The result is the partial derivative . See Example 2.

If you use nested diff calls, the system internally converts them into a single diff call with multiple arguments. See Example 3.

When computing the second and higher derivatives, use the sequence operator as a shortcut. If n is a nonnegative integer, diff(f, x $ n) returns the nth derivative of f with respect to x. See Example 4.

The indeterminates x, x1, x2, ... must be identifiers of domain type DOM_IDENT or indexed identifiers of the form x[n] where x is an identifier and n is an integer. If any indeterminate comes in any other form, MuPAD returns an unresolved diff call. See Example 5.

If f is an arithmetical expression, diff returns an arithmetical expression. If f is a polynomial, diff returns a polynomial. See Example 6.

If the system cannot compute the derivative, it returns an unresolved diff call. See Example 7.

MuPAD assumes that partial derivatives with respect to different indeterminates commute. The function calls diff(f, x1, x2) and diff(f, x2, x1) produce the same result diff(f, y1, y2). Here [y1, y2] = sort([x1, x2]). See Example 8.

MuPAD provides two functions, diff and D, for computing derivatives. Use the differential operator D to compute the derivatives of functions. Use the diff function to compute the derivatives of arithmetical expressions. Mathematically, D(f)(x) coincides with diff(f(x), x) and D([1, 2], f)(x, y) coincides with diff(f(x, y), x, y). You can convert symbolic calls of D to the calls of diff and vice versa by using rewrite. See Example 10.

You can extend the functionality of diff for your own special mathematical functions via overloading. This approach works by turning the corresponding function into a function environment and implementing the differentiation rule for the function as the "diff" slot of the function environment.

If a subexpression of the form g(..) occurs in f, and g is a function environment, then diff(f, x) attempts to call the "diff" slot of g to determine the derivative of g(..).

The system calls the "diff" slot with the arguments g(..), x.

If g does not have a "diff" slot, then the system function diff returns the symbolic expression diff(g(..), x) for the derivative of the subexpression.

The system always calls the "diff" slot with exactly two arguments. If you call the diff function with more indeterminates (for example, if you compute a higher derivative), then MuPAD calls the "diff" slot several times. Each call computes the derivative with respect to one indeterminate. The system caches the results of the calls of "diff" slots in diff in order to prevent redundant function calls. See Example 11.

Similarly, if an element d of a library domain T occurs as a subexpression of f, then diff(f, x) calls the slot T::diff(d, x) to compute the derivative of d.

If the domain T does not have a "diff" slot, then diff considers this object as a constant and returns 0 for the corresponding subexpression.

Examples

Example 1

Compute the derivative of x2 with respect to x:

diff(x^2, x)

Example 2

You can differentiate with respect to multiple variables within a single diff call. For example, differentiate this expression with respect to x, and then with differentiate the result with respect to y:

diff(x^2*sin(y), x, y) = diff(diff(x^2*sin(y), x), y)

Example 3

MuPAD internally converts nested diff calls into a single diff call with multiple arguments:

diff(diff(f(x, y), x), y)

Example 4

Use the sequence operator $ as a shortcut to compute the third derivative of this expression with respect to x:

diff(sin(x)*cos(x), x $ 3)

Example 5

You can differentiate with respect to an indexed identifier. For example, differentiate this expression with respect to x[1]:

diff(x[1]*y + x[1]*x[r], x[1])

Example 6

You can differentiate polynomials with respect to the polynomial indeterminates or the parameters in the coefficients. For example, differentiate this polynomial with respect to the indeterminate x:

diff(poly(sin(a)*x^3 + 2*x, [x]), x)

Now differentiate the same polynomial with respect to its symbolic parameter a:

diff(poly(sin(a)*x^3 + 2*x, [x]), a)

Example 7

MuPAD returns the derivative of an unknown function as an unresolved diff call:

diff(f(x) + x, x)

Example 8

MuPAD assumes that all partial derivatives with respect to different indeterminates commute. Therefore, the system can change the order of indeterminates:

diff(f(x, y), x, y) = diff(f(x, y), y, x);

Example 9

You can use diff to differentiate symbolic integrals. For example, compute the second derivative of this indefinite integral:

F1 := int(f(x), x):
diff(F1, x, x)

Now compute the derivative of the definite integral:

F2 := int(f(t, x), t = x..x^2):
diff(F2, x)

Example 10

Use the operator D to compute the derivatives of functions. Use the diff function to compute the derivatives of expressions:

D(sin), diff(sin(x), x)

Applying D to expressions and diff to functions makes no sense:

D(sin(x)), diff(sin, x)

Use the rewrite function to rewrite an expression replacing the operator D with the diff function:

rewrite(D(f)(x), diff), rewrite(D(D(f))(x), diff)

Also, use rewrite to rewrite an expression replacing diff with D:

diff(f(x, x), x) = rewrite(diff(f(x, x), x), D)

Example 11

You can extend diff to your own special functions. To do so, embed your function, f, into a function environment, g, and implement the behavior of diff for this function as the "diff" slot of the function environment.

If a subexpression of the form g(..) occurs in an expression f, then diff(f, x) calls g::diff(g(..), x) to determine the derivative of the subexpression g(..).

This example demonstrates extending diff to the exponential function. Since the function environment exp already has a "diff" slot, call the new function environment Exp to avoid overwriting the existing system function exp.

Here, the "diff" slot implements the chain rule for the exponential function. The derivative is the product of the original function call and the derivative of the argument:

Exp := funcenv(Exp):
Exp::diff := proc(f, x)
begin
   // f = Exp(something), i.e., something = op(f, 1)
   f*diff(op(f, 1), x):
end_proc:
diff(Exp(x^2), x)

The report created by prog::trace shows one call to Exp::diff with two arguments. Instead of calling Exp::diff twice, the system reads the required result of the second call from an internal cache for intermediate results in diff:

prog::trace(Exp::diff):
diff(Exp(x^2), x, x)
enter Exp::diff(Exp(x^2), x)
computed 2*x*Exp(x^2)

prog::untrace(Exp::diff):  delete f, Exp:

Parameters

f

An arithmetical expression or a polynomial of type DOM_POLY

x, x1, x2, …

Indeterminates: identifiers or indexed identifiers

Overloaded By

f

See Also

MuPAD Functions

Related Examples

Was this topic helpful?