InterpolatingPolynomial[{#, (Sin[5. + #] -
Sin[5.]) / #} & /@ (2.^-Range[0, 6]), 0]
>> 0.2836621854529269
Compare with:
Needs["NumericalCalculus`"]
ND[Sin[x], x, 5]
>> 0.2836621854529268
Interestingly, the one-line implementation in Mathematica is quite a bit
faster than ND! :
Do[
InterpolatingPolynomial[{#, (Sin[5. + #] -
Sin[5.]) / #} & /@ (2.^-Range[0, 6]), 0]
,
{10000}
] // Timing
>> {1.392, Null}
Do[ND[Sin[x], x, 5], {10000}] // Timing
>> {7.561, Null}
Part of this is due to extra overheads for ND like processing user input and
options. Even taking this into account, however, the
InterpolatingPolynomial-based implementation above is still about twice as
fast. I've modfied my NDerivative package to use this method instead of
NumericalCalculus`ND and I've also added a drop-in replacement for ND,
called FastND.
Do[FastND[Sin[x], x, 5], {10000}] // Timing
>> {4.046, Null}
If you want to use NDerivative or FastND, you can get them from
http://andrew.j.moylan.googlepages.com/mathematica.