Q:

Get derivative of a function

I want to write a macro, which takes a mathematical function as argument and returns a plot using pgfplots of the function and of its derivative. 
I guess that this isn't possible using just LaTeX; however, for example, pgfplots can use the external program gnuplot to plot functions or there is LuaTeX...
So is there any way to get the derivative of a mathematical function do do this?

A:

Within reason (i.e. for not too nonlinear functions), you can numerically differentiate your functions right within PGFPlots, using the approach f'(x)=(f(x+dx)-f(x))/dx:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[no markers, legend pos=south east, legend entries={Original function, Analytical derivative, Numerical derivative}]
\addplot [gray] {x^3};
\addplot [line width=3pt, red!50] {3*x^2};
\addplot [black] {((x+0.01)^3-(x)^3)/0.01};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[no markers, samples=500]
\addplot [gray] {sin(deg(x))};
\addplot [line width=3pt, red!50] {cos(deg(x))};
\addplot [black] {(sin(deg(x+0.01))-sin(deg(x)))/0.01};
\end{axis}
\end{tikzpicture}

\end{document}

A:

To derive a function analytically LaTeX is not really equipped with a linear algebra package. Instead you can use sympy, a python library for symbolic mathematics. An example session in python looks like this:
>>> from sympy import *
>>> x = Symbol('x')
>>> diff(sin(x), x)
cos(x)

Thanks to the great pythontex package this can easily be integrated into a LaTeX document. Have a look at the examples, which contain several derivatives of functions and plots using matplotlib. It is no problem though to adapt the output to pgfplots.

A:

the first 15 derivations of
f(x)=1-x^2/2+x^4/24-x^6/720+x^8/40320-x^10/3628800+x^12/479001600-x^14/87178291200

which is 

\PassOptionsToPackage{dvipsnames,svgnames}{pstricks}
\documentclass[pstricks]{standalone}
\usepackage{pstricks-add}
\begin{document}

\psset{unit=2cm}
\def\getColor#1{\ifcase#1 Tan\or RedOrange\or magenta\or yellow\or green%
  \or Orange\or blue\or DarkOrchid\or BrickRed\or Rhodamine\or OliveGreen%
  \or Goldenrod\or Mahogany\or OrangeRed\or CarnationPink\or RoyalPurple\or Lavender\fi}
\begin{pspicture}[showgrid](-0.2,-1.2)(7,1.5)
\psclip{\psframe[linestyle=none](-0.2,-1.1)(7,1.1)}
\multido{\in=0+1}{16}{%
  \psplot[linewidth=1pt,algebraic=true,linecolor=\getColor{\in}]{0}{7}
    {Derive(\in,1-x^2/2+x^4/24-x^6/720+x^8/40320-x^10/3628800+
            x^12/479001600-x^14/87178291200)}}
\endpsclip
\end{pspicture}

\end{document}

and the first 2 of    sin(x)*cos(x)

