\( \DeclareMathOperator{\abs}{abs} \newcommand{\ensuremath}[1]{\mbox{$#1$}} \)

Differentiation With wxMaxima

 1 Basic Introduction

The command to differentiate any expression in Maxima is diff() and it has the following syntax:

diff(expression, variable, [order-of-derivative (only if greater than 1)])

So let's say we want to take the first three derivatives of an 3 sin(x):

--> diff(3*sin(x),x);
\[\tag{%o1} 3 \cos{(x)}\]
--> diff(3*sin(x),x,2);
\[\tag{%o2} -3 \sin{(x)}\]
--> diff(3*sin(x),x,3);
\[\tag{%o3} -3 \cos{(x)}\]

If we have already defined a function f(x), then we can differentiate using the f(x) symbol:

(%i5) f(x):=3*sin(x)$
diff(f(x),x); diff(f(x),x,2); diff(f(x),x,3);
\[\tag{%o3} 3 \cos{(x)}\] \[\tag{%o4} -3 \sin{(x)}\] \[\tag{%o5} -3 \cos{(x)}\]

NOTE: We cannot omit the (x) in the expression f(x), since:

(%i6) diff(f,x);
\[\tag{%o6} 0\]

Maxima thinks that you want the derivative of the symbolic expression f, which has no x dependence,
and therefore yields 0 upon differentiation.

The ev(,nouns) trick works with diff() too:

(%i9) f(x):=3*x^2*tan(5*x^2)$
D:'diff(f(x),x,2)$
D=ev(D,nouns);
\[\tag{%o9} \frac{{{d}^{2}}}{d {{x}^{2}}} \left( 3 {{x}^{2}} \tan{\left( 5 {{x}^{2}}\right) }\right) =600 {{x}^{4}}\, {{\sec{\left( 5 {{x}^{2}}\right) }}^{2}} \tan{\left( 5 {{x}^{2}}\right) }+6 \tan{\left( 5 {{x}^{2}}\right) }+150 {{x}^{2}}\, {{\sec{\left( 5 {{x}^{2}}\right) }}^{2}}\]

 2 Catching the Output in a New Function

Let's say we want to differentiate a function and then find where its derivative is zero, and also we want to calculate some values of the derivative.  We need to be able to "catch" the derivative of that function as a new function.  Here's how we do that:

(%i10) f(x):= sqrt(x)*exp(x^2+2*x);
\[\tag{%o10} \operatorname{f}(x):=\sqrt{x} \operatorname{exp}\left( {{x}^{2}}+2 x\right) \]
(%i11) define(fp(x),diff(f(x),x));
\[\tag{%o11} \operatorname{fp}(x):=\sqrt{x}\, \left( 2 x+2\right) \, {{\% e}^{{{x}^{2}}+2 x}}+\frac{{{\% e}^{{{x}^{2}}+2 x}}}{2 \sqrt{x}}\]

While this is a little ungainly, most all CASes (like Mathematica and Maple) do this.

So why can't we just define g(x) := diff(f(x),x)?  Let's try it:

(%i13) f(x); g(x):=diff(f(x),x);
\[\tag{%o12} \sqrt{x}\, {{\% e}^{{{x}^{2}}+2 x}}\] \[\tag{%o13} \operatorname{g}(x):=\frac{d}{d x} \operatorname{f}(x)\]
(%i14) g(x);
\[\tag{%o14} \sqrt{x}\, \left( 2 x+2\right) \, {{\% e}^{{{x}^{2}}+2 x}}+\frac{{{\% e}^{{{x}^{2}}+2 x}}}{2 \sqrt{x}}\]

Wait... that looks pretty good.  Infact, there doesn't seem to be anything wrong, until we ask a very simple question:

(%i15) g(1);
\[\mbox{}\\\mbox{diff}\mbox{: second argument must be a variable; found }1\mbox{\ensuremath{\neq}0: g(x=1)}\mbox{ -- an error. To debug this try: debugmode(true);}\]

Ahk!  What happend is that with g(x):=diff(f(x),x) when we ask for g(1)
Maxima interprets this as: diff(f(1),1) and since the second argument of the diff() command must be a variable,
the request fails.  So, the correct syntax is:

(%i19) define(g(x),diff(f(x),x))$ g(1);
\[\tag{%o19} \frac{9 {{\% e}^{3}}}{2}\]

 3 Graphing derivatives

Now that we can catch derivatives in their own functions, we can do all the usual things we would like to with them.
For example, we could graph them along side their functions:

(%i22) f(x):=sin(x);define(g(x),diff(f(x),x)); define(h(x),diff(f(x),x,2));
\[\tag{%o20} \operatorname{f}(x):=\sin{(x)}\] \[\tag{%o21} \operatorname{g}(x):=\cos{(x)}\] \[\tag{%o22} \operatorname{h}(x):=-\sin{(x)}\]
(%i23) wxplot2d([f(x),g(x),h(x)],[x,0,2*%pi],[legend,"sin(x)","Derivative", "Second Derivative"]);
\[\tag{%t23} \]  (Graphics)
\[\tag{%o23} \]
(%i24) kill(all);
\[\tag{%o0} \mathit{done}\]

Let's try another example  in which we plot the first three derivatives of:

(%i2) f(x):=sqrt(cos(x)+2);
\[\tag{%o2} \operatorname{f}(x):=\sqrt{\cos{(x)}+2}\]
(%i7) define(g(x),diff(f(x),x)); define(h(x),diff(f(x),x,2));define(i(x),diff(f(x),x,3));
\[\tag{%o5} \operatorname{g}(x):=-\frac{\sin{(x)}}{2 \sqrt{\cos{(x)}+2}}\] \[\tag{%o6} \operatorname{h}(x):=-\frac{{{\sin{(x)}}^{2}}}{4 {{\left( \cos{(x)}+2\right) }^{\frac{3}{2}}}}-\frac{\cos{(x)}}{2 \sqrt{\cos{(x)}+2}}\] \[\tag{%o7} \operatorname{i}(x):=-\frac{3 {{\sin{(x)}}^{3}}}{8 {{\left( \cos{(x)}+2\right) }^{\frac{5}{2}}}}+\frac{\sin{(x)}}{2 \sqrt{\cos{(x)}+2}}-\frac{3 \cos{(x)} \sin{(x)}}{4 {{\left( \cos{(x)}+2\right) }^{\frac{3}{2}}}}\]
(%i8) wxplot2d([f(x),g(x),h(x),i(x)],
   [x,-%pi,%pi],
   [legend,"Function","d1","d2","d3"],
[title, "Three Derivatives of A Function"]);
\[\tag{%t8} \]  (Graphics)
\[\tag{%o8} \]
(%i9) kill(all);
\[\tag{%o0} \mathit{done}\]

 3.1 A Streamlined Way to Get Higher Order Derivatives

A different way to get higher order derivatives is built on exploiting a feature of
the programming language that Maxima is built on.  Lisp (the programming language) allows
a user to define an array (or list of objects) based on a rule or algorithm.  This is
miles away from C/C++, but it allows us a tremendous amount of flexibility.  We'll start an empty list:

(%i37) f=[];
\[\tag{%o37} f=[]\]

Now, at the 0th position, we'll put the function we want to differentiate:

(%i38) f[0](x):=x*cos(exp(x+1));
\[\tag{%o38} {f_0}(x):=x \cos{\left( \operatorname{exp}\left( x+1\right) \right) }\]

O.k., so here's the magic, we will define the nth element of the list to be the nth derivative of the
0th element!

(%i39) define(f[n](x),diff(f[0](x),x,n));
\[\tag{%o39} {f_n}(x):=\frac{{{d}^{n}}}{d {{x}^{n}}} \left( x \cos{\left( {{\% e}^{x+1}}\right) }\right) \]

Now, let's do some differentiating!

(%i43) f[0](x);f[1](x);f[2](x);f[3](x);
\[\tag{%o40} x \cos{\left( {{\% e}^{x+1}}\right) }\] \[\tag{%o41} \frac{d}{d x} \left( x \cos{\left( {{\% e}^{x+1}}\right) }\right) \] \[\tag{%o42} \frac{{{d}^{2}}}{d {{x}^{2}}} \left( x \cos{\left( {{\% e}^{x+1}}\right) }\right) \] \[\tag{%o43} \frac{{{d}^{3}}}{d {{x}^{3}}} \left( x \cos{\left( {{\% e}^{x+1}}\right) }\right) \]

Wait!  Stop!  While symbolically that's right, that's not very useful!  We want the actual expanded out expressions!  Let's get one using the ev() command.  The issue is that the recursion in the definition is at the symbolic level, and we need to force
the evaluation of the diff operator!

(%i44) ev(f[1](x),diff);
\[\tag{%o44} \cos{\left( {{\% e}^{x+1}}\right) }-x\, {{\% e}^{x+1}} \sin{\left( {{\% e}^{x+1}}\right) }\]
(%i45) ev(f[2](x),diff);
\[\tag{%o45} -x\, {{\% e}^{x+1}} \sin{\left( {{\% e}^{x+1}}\right) }-2 {{\% e}^{x+1}} \sin{\left( {{\% e}^{x+1}}\right) }-x\, {{\% e}^{2 x+2}} \cos{\left( {{\% e}^{x+1}}\right) }\]

There's no reason to go in order!

(%i47) ev(f[6](x),diff);
\[\tag{%o47} -15 x\, {{\% e}^{5 x+5}} \sin{\left( {{\% e}^{x+1}}\right) }-6 {{\% e}^{5 x+5}} \sin{\left( {{\% e}^{x+1}}\right) }+90 x\, {{\% e}^{3 x+3}} \sin{\left( {{\% e}^{x+1}}\right) }+150 {{\% e}^{3 x+3}} \sin{\left( {{\% e}^{x+1}}\right) }-x\, {{\% e}^{x+1}} \sin{\left( {{\% e}^{x+1}}\right) }-6 {{\% e}^{x+1}} \sin{\left( {{\% e}^{x+1}}\right) }-x\, {{\% e}^{6 x+6}} \cos{\left( {{\% e}^{x+1}}\right) }+65 x\, {{\% e}^{4 x+4}} \cos{\left( {{\% e}^{x+1}}\right) }+60 {{\% e}^{4 x+4}} \cos{\left( {{\% e}^{x+1}}\right) }-31 x\, {{\% e}^{2 x+2}} \cos{\left( {{\% e}^{x+1}}\right) }-90 {{\% e}^{2 x+2}} \cos{\left( {{\% e}^{x+1}}\right) }\]

We are still stuck with the issue of not having an easy to use function for, say, the 4th or 8th derivative.  Here is where define() comes to the rescue!

(%i54) define(g[4](x),ev(f[4](x),diff))$
define(g[10](x),ev(f[10](x),diff))$

And now we can compute away!

(%i52) ratsimp(g[4](log(%pi)-1));
\[\tag{%o52} \left( 7 {{\ensuremath{\pi} }^{2}}-{{\ensuremath{\pi} }^{4}}\right) \log{\left( \ensuremath{\pi} \right) }+{{\ensuremath{\pi} }^{4}}+5 {{\ensuremath{\pi} }^{2}}\]
(%i55) ratsimp(g[10](log(%pi)-1));
\[\tag{%o55} \left( {{\ensuremath{\pi} }^{10}}-750 {{\ensuremath{\pi} }^{8}}+22827 {{\ensuremath{\pi} }^{6}}-34105 {{\ensuremath{\pi} }^{4}}+511 {{\ensuremath{\pi} }^{2}}\right) \log{\left( \ensuremath{\pi} \right) }-{{\ensuremath{\pi} }^{10}}+390 {{\ensuremath{\pi} }^{8}}+3633 {{\ensuremath{\pi} }^{6}}-43595 {{\ensuremath{\pi} }^{4}}+2039 {{\ensuremath{\pi} }^{2}}\]
(%i56) kill(all);
\[\tag{%o0} \mathit{done}\]

 4 Getting Numerical Values

What if you want a decimal approximation for your derivatives?  We simply wrap the exact value in a float() command:

(%i1) f(x):=3*cos(3*x^2 - exp(sqrt(x)));
\[\tag{%o1} \operatorname{f}(x):=3 \cos{\left( 3 {{x}^{2}}-\operatorname{exp}\left( \sqrt{x}\right) \right) }\]
(%i2) define(g(x),diff(f(x),x));
\[\tag{%o2} \operatorname{g}(x):=-3 \left( \frac{{{\% e}^{\sqrt{x}}}}{2 \sqrt{x}}-6 x\right) \sin{\left( {{\% e}^{\sqrt{x}}}-3 {{x}^{2}}\right) }\]
(%i3) g(2);
\[\tag{%o3} -3 \left( \frac{{{\% e}^{\sqrt{2}}}}{{{2}^{\frac{3}{2}}}}-12\right) \sin{\left( {{\% e}^{\sqrt{2}}}-12\right) }\]
(%i4) float(g(2));
\[\tag{%o4} -31.62025556620378\]

If you are only going to be asking for one value from the derivative, then it may be faster to simply substitute it
directly as a float value:

(%i5) f(x):=(x-1)*exp(-x/2);
\[\tag{%o5} \operatorname{f}(x):=\left( x-1\right) \operatorname{exp}\left( \frac{-x}{2}\right) \]
(%i7) subst(x=2, diff(f(x),x));subst(x=2.0,diff(f(x),x));
\[\tag{%o6} \frac{{{\% e}^{-1}}}{2}\] \[\tag{%o7} 0.18393972058572\]

If you are going to be asking for alot of decimal output from your functions, it might be a good
idea to force the float() command into the definition:

(%i8) f(x);
\[\tag{%o8} \left( x-1\right) \, {{\% e}^{-\frac{x}{2}}}\]
(%i9) define(g(x),float(diff(f(x),x)));
\[\tag{%o9} \operatorname{g}(x):=\frac{1}{{{2.718281828459045}^{\frac{x}{2}}}}-\frac{0.5 \left( x-1.0\right) }{{{2.718281828459045}^{\frac{x}{2}}}}\]
(%i12) g(1);g(2);g(3/2);
\[\tag{%o10} 0.60653065971263\] \[\tag{%o11} 0.18393972058572\] \[\tag{%o12} 0.35427491455576\]
Created with wxMaxima.