The right way to plot  f'[x]

Consider several attempts to plot the derivative of f[x] below.

f[x_] = Sum[Sin[n x]/n, {n, 20}] ;

This first attempt doesn't work at all.

Plot[D[f[x], x], {x, 0, 8}] ;

The second attempt works, but takes over 11 seconds, because the derivative is derived for every sample point.

Plot[f '[x], {x, 0, 8}] ;

The best version is below,and takes a little more than 1 second.  It works much faster because the derivative is worked out only once (before the derivative is sampled).

Plot[Evaluate[f '[x]], {x, 0, 8}] ;

Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page