Avoiding subtraction for finite difference with log and exp I want to approximate the derivative of f(x) Finite difference f(x)~~(f(x+h)-f(x))/(h) I was taught that the error from the subtraction is blown up for small h.
Johan Patton
Open question
2022-08-19
Avoiding subtraction for finite difference with log and exp I want to approximate the derivative of f(x) Finite difference
I was taught that the error from the subtraction is blown up for small h. This I can verify with MATLAB. Smartass method So I though maybe the following would fix the problem:
However, I get the same relative errors. (Which also start increasing for h smaller than approx. Why is this?
Answer & Explanation
Kaeden Bishop
Beginner2022-08-20Added 15 answers
First of all, you should always be careful to choose with a very slight dependence on such that is exactly machine representable. For instance, if , then you should at least round the you are tempted to use to the nearest multiple of for 32-bit precision or for 64-bit doubles. Then, as Manazoni points out, the symmetric formula is superior; it gets a precision of about two orders of magnitude better working with 64-bit numbers. You should choose an somewhere near , where is the fractional machine precision (thus in 64-bit doubles, of roughly is good. The fractional error in the derivative should be about Yet better, unless you suspect really weird behavior near , is to use the next higher order symmetric formula
using , which gives fractional accuracy of about Higher order methods begin to probe too sensitively for slightly irregular function behavior and begin to rely too heavily on accuracy of the function evaluations. Unfortunately, unlike the case for integration where there are adaptive step size methods, there is no robust method of choosing order and step size that reliably gives you the best achievable estimate of the derivative. But there are methods that are yet better than the one I showed here, see chapter 5.7 of Numerical recipes.
Nashorn0o
Beginner2022-08-21Added 3 answers
Your solution doesn't help as it is essentially the same thing. is not going to be a more accurate representation of than evaluating it directly (on average, I would imagine it will be less accurate). There isn't really much you can do when you are working that close to machine error afaik. Practically speaking, you don't need to use such a tiny with reasonable functions; the practical solution is that when you are dividing by a small number, don't let that number be close to