4 ms·
This general case of this method is sometimes called Lagrangian interpolation. As the article describes, there is one caveat with using these formulas: you can
by eliteraspberrie 12y ago
This general case of this method is sometimes called Lagrangian interpolation. As the article describes, there is one caveat with using these formulas: you can only decrease the step size so much before it backfires.
Because you are dividing by h, to decrease it from, say 1e-6, to 1e-8 introduces more error into the result than it removes. (Floating-point division with a numerator and denominator that have a big difference in magnitude is a problem.)
The example problem in the article just happened to be well conditioned because the sine and cosine, and their derivatives, are bound on [-1, 1], but most problems won't be and the complex step method won't help much then either.
To get better accuracy, rather than decrease the step size, you should use a higher-order formula. The three and five-point formulas are common. The Handbook of Mathematical Functions (Abramowitz & Stegun) has a table of coefficients for differentiation formulas up to the sixth order (Table 25.2): http://imgur.com/eRl8w8h http://imgur.com/eRl8w8h
For fun, derive the 8-point formulas.
- jwmerrill 12y agoNot so. Roundoff errors happen because when you add a big number and a small number in finite precision, the result loses information that was in the small number. The magic of complex step differentiation is keeping the small part (the imaginary part) separate from the (potentially) large part (the value of the function evaluated at a real argument). You can use a very small step size (and so get very high accuracy) with traditional finite difference differentiation at x=0 for functions with f(0) = 0. Complex step differentiation takes advantage of the fact that real valued functions of a real argument have 0 imaginary part everywhere on the real line. > most problems won't be [well conditioned] and the complex step method won't help much then either Try coming up with an example of this. You might be able to find one if there is a pole or branch cut very near the real line, but otherwise, complex step differentiation is very robust.