Pergunta

To calculated the value of the derivative of a given function Func at a given point x, to get a good precision one would think this:

a = Fun( x - Number.MIN_VALUE)
b = Func( x + Number.MIN_VALUE)
return (b-a)/(2*Number.MIN_VALUE)

Now for any x + Number.MIN_VALUE (or x - Number.MIN_VALUE) both return x in javascript.

I tried of different values, and 1 + 1e-15 returns 1.000000000000001. Trying to get more precision, 1 + 1e-16 returns 1. So I'll have to use 1e-15 instead of Number.MIN_VALUE which is 5e-324.

Is there a way to get a better precision in this case in javascript?

Foi útil?

Solução

This is not at all about javascript, actually.

Reducing the separation between the points will not increase your precision of the derivative. The values of the function in the close points will be computed with some error in the last digits, and finally, your error will be much larger than the point separation. Then you divide very small difference which has a huge relative error by a very small number, and you most likely get complete rubbish.

The best way to get a better value of the derivative is to calculate function in multiple points (with not very small separation) and then construct an approximation polynomial and differentiate it in the point of your interest.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top