L-18 MCS 320 Monday 3 October 2005
| > | restart; |
1. Symbolic Differentiation
Symbolic differentiation is done by the "diff" command, and the inert version "Diff". This is the derivation of formulas, as we do on paper.
| > | expsin := exp(sin(x)); |
| > | Diff(expsin,x) = diff(expsin,x); |
The "Diff" only stores the definition of a derivative, while "diff" executes.
| > | T := op(diff) |
| > | ; |
| > | T := op(4,eval(diff |
| > | )); |
We see how Maple stores the results of previous calls. Here we change the remember table:
| > | T[(sin(x),x)] := sin(x); |
| > | diff(sin(x),x); # intended effec |
| > | t |
| > | forget(diff); |
| > | diff(sin(x),x); |
Often the formulas we want to define are implicitly defined by an equation.
Suppose we would like to compute the tangent line to a circle, given as x^2 + y^2 - 1= 0, at some point. We could solve for y, write y as a function of x. But we should actually consider y as a function of x and derive the equation.
| > | alias(y=y(x)); # see y not as the symbol y, but as function of x |
| > | diff(y,x); |
Without the alias, diff(y,x) would have returned zero. We now consider the equation for the circle:
| > | circle := x^2 + y^2 = 1; |
| > | equ := diff(circle,x); |
| > | solve(equ,diff(y,x)); |
The short way to compute the derivative of a formula defined by implicitly by an equation is
| > | implicitdiff(x^2 + z^2=1,z,x); |
Here we choose z as second variable, because y = y(x). With implicitdiff, we do not need to use an alias. We just take the equation as first argument.
| > | x$10; # the $ operator creates sequences |
to be used for repeated differentiation, say the 10-th derivative:
| > | diff(expsin,x$10); |
![]()
![]()
![]()
| > | implicitdiff(x^2+z^2=1,z,x$10); # to get the 10-th derivative |
2. Automatic Differentation
Automatic differentation is the differentation of procedures. Typically this is done by the command "D", although the quiz 6 with piecewise is also an example of automatic differentiation.
The result of automatic differentiation is again a procedure:
| > | f := unapply(expsin,x); |
| > | df := D(f); |
| > | df(1.2); df(z); |
| > | df10 := D[1$10](f); # 10-th derivative |
![]()
![]()
![]()
![]()
The [1$10] means: apply D ten times to the first variable.
| > | df10(x); |
![]()
![]()
![]()
This corresponds to the symbolic differentation.
Before going to an application, let us look at some notation:
| > | diff(cos(t),t); |
| > | diff(cos,t); |
| > | D(cos(t)); |
| > | D(cos); |
Only the first and the fourth command give correct results. Notice that the first result is a formula, while the fourth result is a function.
As an application of automatic differentation, we consider a case where evaluating the derivative is easier than evaluating the symbolic expression.
| > | jf := z -> z^2 + 1/4; |
| > | jf10 := jf@@10; |
| > | jf10(3.2); |
Applying the quadratic map 10 times with jf@@10 leads to a function which is easy to evaluate.
| > | sf10 := jf10(z); |
If we begin to differentiate, we encounter expression swell:
| > | djf := diff(sf10,z); |



The symbolic derivative starts to get huge.
| > | Djf := D(jf10); # automatic differentiation |
If we are only interested in the value of the derivative, i.e.: if we want to know how fast the function increase at a certain point.
| > | Djf(3.2); |
The alternative is to take the symbolic derivative and turn this into a procedure via the unapply.
| > | sdjf := unapply(djf,z); |



| > | eval(Djf); |
| > | eval(sdjf); |



While both Djf and sdjf will give the same numbers, they are the same procedures, we see that the result of automatic differentiation is much shorter than the procedure obtained via symbolic differentiation.