diff (and Diff)

The
diff command is used to compute derivatives of Maple expressions. The syntax of diff is Maple's usual

diff(what,how);

syntax.  "What" in this case refers to "take the derivative of what?", and "how" to "with respect to what variable?".  For example, to compute

diff((3*x-6)/(x^2-4),x)

we would enter:

>   restart:

>   diff((3*x-6)/(x^2-4),x);

3/(x^2-4)-2*(3*x-6)/(x^2-4)^2*x

Notice, the first argument of diff is the expression whose derivative is being taken, and the second tells with respect to what variable the derivative is being taken .  This second part becomes crucial in expressions such as:

>   diff(exp(a*x),x);

a*exp(a*x)

where there are constants, parameters or other variables around. Maple assumes that you mean to take the derivative as the variable you specify changes, and that all other letters in the expression represent constants (but see the sections below on implicit differentiation and partial derivatives).

Maple can deal "theoretically" with derivatives of functions that do not yet have explicit definitions, For example, you may not have yet defined f(x) or g(x), but Maple can tell you that:

>   diff(f(x)*g(x),x);

diff(f(x),x)*g(x)+f(x)*diff(g(x),x)


Notice that Maple uses the "rounded d" (partial derivative sign) for derivatives. This is a (reasonable) convention that was chosen by the people who produced the software.

Higher derivatives:  There is a special notation for second, third, etc.. derivatives. Instead of typing:

>   diff(diff(3*sin(x),x),x);

-3*sin(x)

for the second derivative of 3sin(x), you may use either of the following (the second is easiest to type):

>   diff(3*sin(x),x,x);

-3*sin(x)

>   diff(3*sin(x),x$2);

-3*sin(x)

There are obvious extensions to this, using x$3, x$4, etc..

Examples: Here are a few examples of standard uses of derivatives, which combine diff with other basic Maple commands:

1. Find the slope of the tangent line to the graph of y=3*x/(x-2) at the point (x=3, y=9).
Solution: First define the variable y to be equal to the expression:

>   y:=3*x/(x-2);

y := 3*x/(x-2)

Then take the derivative (we choose the name dy for the derivative):

>   dy:=diff(y,x);

dy := 3/(x-2)-3*x/(x-2)^2

Then substitute x=3 into the expression for the derivative of y:

>   subs(x=3,dy);

-6

The slope at that point is -6. We can use this information to plot the graph of y together with its tangent line at (x=3,y=9) as follows:

>   tangentline:=9-6*(x-3);

tangentline := 27-6*x

>   plot([y,tangentline],x=2.5..4,color=[green,red],thickness=2);

[Maple Plot]


2. Find the maximum and minimum of the function
x*exp(-x) on the (closed) interval x=0..3.
Solution:  Since we are using Maple, we plot first and calculate later:

>   y:=x*exp(-x);

y := x*exp(-x)

>   plot(y,x=0..3,color=blue,thickness=2);

[Maple Plot]

It looks like the minimum of y is zero (at x=0) and the maximum occurs for x=1 (which would make it 1/e). To verify this, we do the calculations. First we need to find the critical points of y (i.e., take the derivative and set it equal to zero):

>   dy:=diff(y,x);

dy := exp(-x)-x*exp(-x)

>   solve(dy=0,x);

1

This verifies our earlier observation that there is one critical point in the interval, at x=1. Now, we need to evaluate y at the critical point, and at the endpoints of the interval:

>   subs(x=0,y), subs(x=1,y), subs(x=3,y);

0, exp(-1), 3*exp(-3)

We know which is the biggest and which is smallest from the plot, but it doesn't hurt to verify this numerically:

>   evalf(%);

0., .3678794412, .1493612051

To summarize, the maximum value of y is 1/e, which occurs when x=1, and the minimum is 0 which occurs when x=0.

Implicit differentiation:  Maple knows how to take the derivative of both sides of an equation. As illustrated above (with the product rule), Maple can also "theoretically" take derivatives of functions whose definitions are not specified. What must be specified explicitly is the dependence of the function on its variable (This means that if you are thinking of y as being a function of x, you must write "y(x)" in your equations rather than just "y").

Here is a typical implicit differentiation problem:

3. The variables x and y are related by the equation
x^2*y(x)-3*y(x)^3*x = 0. Find the slope of the graph of this equation at the point where x=3 and y=1.
Solution:   First, define the equation, where we are considering y as a function of x (i.e., x is the independent variable and y is the dependent one):

>   restart;

>   eq:=x^2*y(x) - 3*y(x)^3*x = 0;

eq := x^2*y(x)-3*y(x)^3*x = 0

It is crucial to write y(x) everywhere. Now, we can take the derivative of both sides:

>   deq:=diff(eq,x);

deq := 2*x*y(x)+x^2*diff(y(x),x)-9*y(x)^2*x*diff(y(x),x)-3*y(x)^3 = 0


This gives the relationship among x, y and the derivative of y with respect to x. To solve the problem, we need to solve this equation for the derivative of y and then substitute x=3 and y=1 into the solution:

>   solve(deq,diff(y(x),x));

-y(x)*(2*x-3*y(x)^2)/x/(x-9*y(x)^2)

>   subs(y(x)=1,x=3,%);

1/6

(Note: The order in which we did things, i.e., solve then substitute, is important, because substitution into the differentiated equation deq will replace pieces of the expression diff(y(x),x) with numbers -- which would be premature. Try it to see what goes wrong.  You might also see what goes wrong if you replace the substitution statement above with "subs(x=3,y(x)=1,%);".  This is a very good example which shows that substitutions are done from left to right.)

There is another way to do implicit differentiation by using the command "implicitdiff". Its syntax is:  

> implicitdiff(f,y,x);

where f is the expression that you want to differentiate, y the dependent variable, and x the independent variable.

For example:

>   implicitdiff(x^2*y-3*y^3*x=0,y,x);

-y*(2*x-3*y^2)/x/(x-9*y^2)

>   subs(y=1,x=3,%);

1/6

For more help on implicitdiff, type

>   ?implicitdiff


Remarks:  Occasionally, to make your worksheets easier to read, you may wish to have Maple display a derivative in standard mathematical notation without evaluating it. For this there is a capitalized, "inert" form of the diff command:

>   Diff(exp(x)/(1-x),x);

Diff(exp(x)/(1-x),x)

Sometimes, you can use the two forms together to produce meaningful sentences:

>   Diff(exp(x)/(1-x),x)=diff(exp(x)/(1-x),x);

Diff(exp(x)/(1-x),x) = exp(x)/(1-x)+exp(x)/(1-x)^2

---------------------------------------------------------------


Partial derivatives: Toward the end of Math 104, you will learn how to take "partial derivatives" of functions of several variables. Maple can take partial derivatives as well as ordinary ones -- the same diff command is used for this. It is here that the "how" part of the diff command is especially important.


For example, here is a function of two variables:

>   f:=x^3*exp(y)-sin(x*y);

f := x^3*exp(y)-sin(x*y)

To take the partial derivative of f with respect to x, we use the command (notice the use of the capitalized form of the diff command):

>   Diff(f,x)=diff(f,x);

Diff(x^3*exp(y)-sin(x*y),x) = 3*x^2*exp(y)-cos(x*y)*y

For the partial derivative with respect to y, we would use:

>   Diff(f,y)=diff(f,y);

Diff(x^3*exp(y)-sin(x*y),y) = x^3*exp(y)-cos(x*y)*x

To take higher-order partial derivatives, we simply list the sequence of variables with respect to which to take the derivative:

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

Diff(x^3*exp(y)-sin(x*y),`$`(x,2),y) = 6*x*exp(y)+cos(x*y)*x*y^2+2*sin(x*y)*y


----------------------------------------------------------------

Few things can go wrong using the
diff command, other than syntax errors -- except possibly that sometimes the variable in the command (the x in "diff(... ,x)") has already been given a value that you forgot about:

>   x:=3;

x := 3

>   diff(x^2/sin(2*x^2),x);

Error, wrong number (or type) of parameters in function diff

Here, diff  is objecting to your trying to take the derivative "with respect to 3".

Another common error is to forget the "how" (x) part entirely: For instance

>   diff(3*t^5);

Error, wrong number (or type) of parameters in function diff

instead of

>   diff(3*t^5,t);

15*t^4