Documentation

# `'`, `D`

Differential operator for 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.

## Syntax

```f '
D(`f`)
D(`[n1, n2, …]`, `f`)
```

## Description

`D(f)` or, alternatively, `f'` computes the derivative of the univariate function `f`.

`D([n1, n2, ...], f)` computes the partial derivative of the multivariate function f(x1, x2, …).

MuPAD® has two functions for differentiation: `diff` and `D`. `D` represents the differential operator that may be applied to functions; `diff` is used to differentiate arithmetical expressions. Mathematically, `D(f)(x)` coincides with ```diff(f(x), x)```; `D([1, 2], f)(x, y)` coincides with ```diff(f(x, y), x, y)```. Symbolic calls of `D` and `diff` can be converted to one another via `rewrite`. Cf. Example 8.

`D(f)` returns the derivative of the univariate function`f`. `f'` is shorthand for `D(f)`.

If `f` is a multivariate function and denotes the partial derivative of `f` with respect to its n-th argument, then `D([n1, n2, ...], f)` computes the partial derivative . Cf. Example 5. In particular, ```D([ ], f)``` returns `f` itself.

### Note

It is assumed that partial derivatives commute. Internally, ```D([n1, n2, ...], f)``` is converted to `D([m1, m2, ...], f)`, where `[m1, m2, ...]` = `sort([n1, n2, ...])`.

`f` may be any object which can represent a function. In particular, `f` may be a functional expression built from simple functions by means of arithmetic operators (`+`, `-`, `*`, `/`, `^`, `@`, `@@`). Any identifier different from CATALAN, EULER, and PI is regarded as an “unknown” function; the same holds for elements of kernel domains not explicitly mentioned on this page. Cf. Example 1. Any number and each of the three constant identifiers above is regarded as a constant function. Cf. Example 2.

If `f` is a list, a set, a table, or an array, then `D` is applied to each entry of `f`. Cf. Example 3.

A polynomial `f` of type `DOM_POLY` is regarded as polynomial function, the indeterminates being the arguments of the function. Cf. Example 6.

If `f` is a function environment, a procedure, then `D` can compute the derivative in some cases; see the “Background” section below. If this is not possible, a symbolic `D` call is returned.

Higher partial derivatives `D([n1], D([n2], f))` are simplified to `D([n1, n2], f)`. Cf. Example 7.

The derivative of a univariate function f —denoted by `D(f)`— is syntactically distinguished from the partial derivative `D([1], f)` with respect to the first variable, even if f represents a univariate function.

The usual rules of differentiation are implemented:

• `D(f + g) = D(f) + D(g)`,

• `D(f * g) = f * D(g) + g * D(f)`,

• `D(1/f) = -D(f) / f^2`,

• `D(f @ g) = D(f) @ g * D(g)`.

Note that the composition of functions is written as `f@g` and not as `f(g)`.

In order to express the n-th derivative of a univariate function for symbolic n, you can use the “repeated composition operator” `@@`. Cf. Example 9.

## Environment Interactions

`D` uses `option remember`.

## Examples

### Example 1

`D(f)` computes the derivative of the function `f`:

`D(sin), D(x -> x^2), D(id)`

`D` also works for more complex functional expressions:

`D(sin @ exp + 2*(x -> x*ln(x)) + id^2)`

If `f` is an identifier without a value, a symbolic `D` call is returned:

`delete f: D(f + sin)`

The same holds for objects of kernel type that cannot be regarded as functions:

`D(NIL)`

`f'` is shorthand for `D(f)`:

`(f + sin)', (x -> x^2)', id'`

### Example 2

Constants are regarded as constant functions:

`PI', 3', (1/2)'`

### Example 3

The usual rules of differentiation are implemented. Note that lists and sets may also be taken as input; in this case, `D` is applied to each element of the list or set:

`delete f, g: D([f+g, f*g]); D({1/f, f@g})`

### Example 4

The derivatives of most special functions of the library can be computed. Again, `id` denotes the identity function:

`D(tan); D(sin*cos); D(1/sin); D(sin@cos); D(2*sin + ln)`

### Example 5

`D` can also compute derivatives of procedures:

```f := x -> x^2: g := proc(x) begin tan(ln(x)) end: D(f), D(g)```

We differentiate a function of two arguments by passing a list of indices as first argument to `D`. In the example below, we first differentiate with respect to the second argument and then differentiate the result with respect to the first argument:

`D([1, 2], (x, y) -> sin(x*y))`

The order of the partial derivatives is not relevant:

`D([2, 1], (x, y) -> sin(x*y))`

`delete f, g:`

### Example 6

A polynomial is regarded as a polynomial function:

`D(poly(x^2 + 3*x + 2, [x]))`

We differentiate the following bivariate polynomial `f` twice with respect to its second variable `y` and once with respect to its first variable `x`:

```f := poly(x^3*y^3, [x, y]): D([1, 2, 2], f) = diff(f, y, y, x)```

`delete f:`

### Example 7

Nested calls to `D` are flattened:

`D([1], D([2], f))`

However, this does not hold for calls with only one argument, since `D(f)` and `D([1], f)` are not considered to be the same:

`D(D(f))`

### Example 8

`D` may only be applied to functions whereas `diff` makes only sense for expressions:

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

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

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

`rewrite` allows to rewrite expressions with `D` into `diff`-expression:

`rewrite(D(f)(y), diff), rewrite(D(D(f))(y), diff)`

The reverse conversion is possible as well:

`map(%, rewrite, D)`

### Example 9

Sometimes you may need the n-th derivative of a function, where n is unknown. This can be achieved using the repeated composition operator. For example, let us write a function that computes the k-th Taylor polynomial of a function f at a point x0 and uses x as variable for that polynomial:

```kthtaylorpoly:= (f, k, x, x0) -> _plus(((D@@n)(f)(x0) * (x - x0)^n / n!) \$ n = 0..k): kthtaylorpoly(sin, 7, x, 0)```

`delete kthtaylorpoly:`

### Example 10

Advanced users can extend `D` to their own special mathematical functions (see “Background” section below). To this end, embed your mathematical function f, say, into a function environment`f` and implement the behavior of `D` for this function as the `"D"` slot of the function environment. The slot must handle two cases: it may be either called with only one argument which equals `f`, or with two arguments where the second one equals `f`. In the latter case, the first argument is a list of arbitrary many indices; that is, the slot must be able to handle higher partial derivatives also.

Suppose, for example, that we are given a function f(t, x, y), and that we do not know anything about f except that it is differentiable infinitely often and satisfies the partial differential equation . To make MuPAD eliminate derivatives with respect to `t`, we can do the following:

```f := funcenv(f): f::D := proc(indexlist, ff) local n : DOM_INT, // Number of t-derivatives. list_2_3 : DOM_LIST; // List of indices of 2's and 3's. // These remain unchanged. begin if args(0) <> 2 then error("Wrong number of arguments") end_if; n := nops(select(indexlist, _equal, 1)); list_2_3 := select(indexlist, _unequal, 1); // rewrite (d/dt)^n = (d^2/dx^2 + d^2/dy^2)^n _plus(binomial(n, k) * hold(D)(sort([2 \$ 2*(n-k), 3 \$ 2*k].list_2_3), ff) \$ k = 0..n) end_proc:```

Now, partial derivatives with respect to the first argument t are rewritten by derivatives with respect to the second and third argument:

`D([1], f^2)(t, x, y)`

`D([1, 2, 1], f)`

`delete f:`

## Parameters

 `f` A function or a functional expression, an array, a list, a polynomial, a set, or a table `n1, n2, …` Indices: positive integers

## Return Values

function or a functional expression. If `f` is an array or a list etc., a corresponding object containing the derivatives of the entries is returned.

`f`

## Algorithms

If `f` is a domain or a function environment with a slot`"D"`, this slot is called to compute the derivative. The slot procedure has the same calling syntax as `D`. In particular —and in contrast to the slot`"diff"`— the slot must be able to compute higher partial derivatives because the list of indices may have length greater than one. Cf. Example 10.

If `f` is a procedure, a function environment without a `"D"` slot, then `f` is called with auxiliary identifiers as arguments. The result of the call is then differentiated using the function `diff`. If the result of `diff` yields an expression which can be regarded as function in the auxiliary identifers, then this function is returned, otherwise an unevaluated call of `D` is returned.

Let us take the function environment`sin` as an example. It has no `"D"` slot, thus the procedure ```op(sin, 1)```, which is responsible for evaluating the sine function, is used to compute `D(sin)`, as follows. This procedure is applied to an auxiliary identifier, say `x`, and differentiated with respect to this identifier via `diff`. The result is ```diff(sin(x), x) = cos(x)```. Via `fp::expr_unapply` and `fp::unapply`, the function `cos` is computed as the derivative of `sin`.