next up previous index
Next: Fitting Up: Functions Previous: Defining a Function

Differentiating a Function

Functions in symbol manipulation systems such as Maxima, Maple and Mathematica are more like real mathematical functions and less like functions in Fortran or in C. In particular, because they are defined in terms of symbolic expressions, operations such as symbolic differentiation and integration can be performed on them.

Consider the following function:

\begin{displaymath}F : x \mapsto x \sin{a x} + b x^2

where a and b are constants. Its derivative is:

\begin{displaymath}\frac{\textrm{d} F}{\textrm{d} x} = \sin{a x} + a x \cos{a x} + 2 b x

In order to obtain this result with Maxima invoke Maxima command diff: 

\begin{tex2html_preform}\begin{verbatim}(C5) f(x) := x * sin(a * ...
...A X) + A X COS(A X) + 2 B X

The second derivative of F(x) is

\begin{displaymath}\frac{\textrm{d}^2 F}{\textrm{d} x^2} =
- a^2 x \sin{a x} + 2 a \cos{a x} + 2 b

To obtain this result with Maxima you can either invoke diff on the result returned by the first diff, or invoke diff with three arguments, where the third argument is the order of the derivative:
\begin{tex2html_preform}\begin{verbatim}(C8) diff(diff(f(x), x), ...
...(A X) + 2 A COS(A X) + 2 B

Maxima also has a delayed  derivative, that is a diff operation, which marks an expression as a derivative, but doesn't evaluate it:

\begin{tex2html_preform}\begin{verbatim}(C28) f(x);
(D28) X SI...
... X) + A X COS(A X) + 2 B X

Here we have forced the evaluation by calling ev(%, diff), where diff is a special ev flag, that tells ev to perform all delayed differentiations in the expression, which is given as the first argument to ev. Maxima supports the following syntactic sugar notation that can be used instead of ev:
\begin{tex2html_preform}\begin{verbatim}(C64) g(x) := 'diff(f(x),...
... X) + A X COS(A X) + 2 B X

We can evaluate the value of dF/dx at a specific point x = 3, say, and for given values of constants a = 1 and b = 2as follows:
\begin{tex2html_preform}\begin{verbatim}(C91) f(x);
(D91) X SI...
(D95) 9.1711425182585291

Now let us have a look at how all this works in Maple. First let us define function F:

\begin{tex2html_preform}\begin{verbatim}> f := x -> x * sin(a*x) ...
f := x -> x sin(a x) + b x>\end{verbatim}\end{tex2html_preform}\end{figure}

Function diff works in Maple much the same as it does in Maxima. There is also a delayed evaluation version of diff, which is called Diff. Recall that a similar relationship between sum and Sum. 
\begin{tex2html_preform}\begin{verbatim}> diff(f(x), x);
sin(a x...
...n(a x) + x cos(a x) a + 2 b x>\end{verbatim}\end{tex2html_preform}\end{figure}

Higher order derivatives in Maple are done differently than in Maxima though. Calling diff(f(x), x, 2) would result in an error message. Instead you have to do this as follows:
\begin{tex2html_preform}\begin{verbatim}> diff(f(x), x$2);
2 ...
...+ 4> evalf(%);

Here we have also found the value of d2 F / d x2at x = 3 and for the values of constants a = 1 and b = 2.

In Mathematica the notation used in this context is painfully different for those who are used to Maxima and/or Maple, but, at the same time, somewhat closer to a traditional notation used in mathematics. The following listing shows a conversation with Mathematica, which begins with the definition of function F. This is followed by taking its derivative and then evaluation of its value at x=3 for a=1 and b=2.

\begin{tex2html_preform}\begin{verbatim}In[1]:= f[x_] = x Sin[a x...
...= N[%]Out[5]= 9.17114In[6]:=\end{verbatim}\end{tex2html_preform}\end{figure}

Higher derivatives in Mathematica are calculated by replacing x in the call to D with a list, of which the first element is x and the second element is the order of the derivative:

\begin{tex2html_preform}\begin{verbatim}In[6]:= D[f[x], {x, 2}]...
...Cos[a x] - a x Sin[a x]In[7]:=\end{verbatim}\end{tex2html_preform}\end{figure}

This is somewhere in between Maxima and Maple notation.

next up previous index
Next: Fitting Up: Functions Previous: Defining a Function
Zdzislaw Meglicki