Derivatives

This notebook gives a brief explanation of how to use Mathematica  to take derivatives and to apply the chain rule properly.  See the Mathematica  book for more details.

Ordinary Derivatives

If a function has been defined, say

f[x_] = x^3 - x^2

then its derivative can be easily calculated using the prime (') notation:

f'[x]

The built in function D[] also calculates the derivative

D[f[x], x]

With the function D[], the variable must be given as the second argument. This function also works on expressions, for example

D[x^3 - x^2, x]

or

y = x^3 - x^2

D[y,x]

Partial Derivatives

The function D[] assumes that every variable except the one given as the second argument is a constant. Consequently, D[] can be used to calculate partial derivatives.

Clear[y]

f[x_,y_] = x^3 y^2 - y

The partial derivative of f[x,y] with respect to x:

D[f[x,y], x]

The partial derivative of f[x,y] with respect to y:

D[f[x,y], y]

As usual, D[] works with expressions:

D[x^3y^2 - y, x]

D[x^3y^2 - y, y]

z = x^3y^2 - y

D[z,x]

D[z,y]

Higher Order Derivatives

To compute higher order derivatives, just give the desired variables after the function:

z = x^3y^2 - y

Second partial derivative with respect to x:

D[z, x,x]

Mixed partial derivatives with respect to x and y:

D[z, x,y]

D[z, y,x]

Repeated variables can abreviated with a list. For example,

D[z, x,x,x]

could be entered as

D[z, {x,3}]

Such abbreviations can be combined. For example, 3 partial derivatives with respect to x and 2 with respect to y could be computed with:

D[z, {x,3}, {y,2}]

Chain Rule

The function D[] will automatically apply the chain rule properly if the dependencies between various variables are explicitly given.

Clear[x,y]

f[x_,y_] = x^2 y + y^3;

D[f[x[t],y[t]], t]

Notice that x[t] and y[t] were used to indicate that they depend on t. If just x and y had been used, then D[] would assume they were independent of t and give 0:

D[f[x,y], t]

If x[t] and y[t] have been defined then these definitions will, of course, be used by Mathematica  to complete the expression for the derivative.

x[t_] = E^t; y[t_] = t^3 + 1;

D[f[x[t],y[t]], t]

Example 2:

Clear[x,y]

f[x_,y_] = x / y;

D[f[x[u,v],y[u,v]], u]

D[f[x[u,v],y[u,v]], v]

Notice that the partial derivatives of x and y with respect to u, the first variable, are indicated by the exponent (1,0). The partial derivatives of x and y with respect to v, the second variable, are indicated by the exponent (0,1). Again, if x[u,v] and y[u,v] are defined, Mathematica  will use the definitions to complete the expression for the derivative.

x[u_,v_] = u v; y[u_,v] = u^2 - v^2;

D[f[x[u,v],y[u,v]], u]

D[f[x[u,v],y[u,v]], v]

Example 3:

The chain rule is important in situations where there is incomplete information about how the variables depend on each other so that the partial derivatives cannot be computed by substituting expressions for the variables into the function as in the previous example.

Let

Clear[x,y]

f[x_,y_] = x^2 Cos[x + y];

Suppose x and y are (unknown) functions of u and v, x[u,v], y[u,v], and x[0,0]=Pi, y[0,0]=0. Suppose that the partial derivatives are known at this point: 

    D[x[u,v],u]-> 3; D[x[u,v],v]->-1;
    D[y[u,v],u]->-2; D[y[u,v],v]->-2;

Then the partial derivatives of f with respect to u and v at the point {u,v}=={0,0} can be computed as follows:

D[f[x[u,v],y[u,v]], u]

Plug into this expression:

% /. {x[u,v]->Pi, y[u,v]->0,
		D[x[u,v],u]->3, D[y[u,v],u]->-2}

D[f[x[u,v],y[u,v]], v]

Plug into this expression:

% /. {x[u,v]->Pi, y[u,v]->0,
		D[x[u,v],v]->-1, D[y[u,v],v]->-2}

Total Derivatives

The function Dt[] computes the "total derivative" of a function. Dt[] treats every variable as if it depended on every other and applies the chain rule accordingly. Explicit dependencies between the variables need not be given in expressions. On the other hand,  the variables that are independent of other variables have to be declared with the option Constants->{}.

Example:

z = x / y;

Dt[z, u]

Dt[z, v]

x = u v; y = u^2 - v^2;

Dt[z, u]

Dt[z, v]

When computing Dt[] Mathematica  assumes that u might depend on v and vice versa. To make v independent of u in this computation, declare v to be constant with respect to u (and vice versa):

Dt[z, u, Constants->{v}]

Dt[z, v, Constants->{u}]

Finally, since Mathematica  automatically substitutes the expressions for x an y into z,  the chain rule can be avoided altogether in computing these partial derivatives:

z

D[z,u]

D[z,v]