实施C / C衍生物++ [英] implementing the derivative in C/C++

查看:115
本文介绍了实施C / C衍生物++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何衍生物的函数f(x)通常计算的编程,以确保最高的准确度?我实施牛顿 - 拉夫逊的方法,它需要采取的函数的导数。

感谢

解决方案

我同意erikkallen的(F(X + H) - F(X - h)条)/ 2H是数值逼近衍生品通常的做法。然而,获得正确的步长h是有点微妙。

在近似误差(函数f(x + H) - F(X - h))进行/小时降低为h变小,这表示,你应该采取H作为越小越好。但为h变小,从浮点减法增加了错误,因为分子需要减去几乎相等。如果h过小,可以在减法宽松了很多precision。因此,在实践中你必须选择的h的不太小的值最小化相结合的逼近的错误和数字的错误。

作为一个经验法则,你可以尝试H = SQRT(DBL_EPSILON),其中DBL_EPSILON是最小的双precision查到,使得1 + E!= 1机precision。 DBL_EPSILON约为10 ^ -15所以你可以使用H = 10 ^ -7或10 ^ -8。

有关详细信息,请参阅以下说明上采摘的步长微分方程。

How is derivative of a f(x) is typically calculated programmatically to ensure maximum accuracy? I am implementing the Newton-Raphson method and it requires taking of the derivative of a function.

thanks

解决方案

I agree with erikkallen that (f(x + h) - f(x - h))/2h is the usual approach for numerically approximating derivatives. However, getting the right step size h is a little subtle.

The approximation error in (f(x + h) - f(x - h))/2h decreases as h gets smaller, which says you should take h as small as possible. But as h gets smaller, the error from floating point subtraction increases since the numerator requires subtracting nearly equal numbers. If h is too small, you can loose a lot of precision in the subtraction. So in practice you have to pick a not-too-small value of h that minimizes the combination of approximation error and numerical error.

As a rule of thumb, you can try h = SQRT(DBL_EPSILON) where DBL_EPSILON is the smallest double precision number e such that 1 + e != 1 in machine precision. DBL_EPSILON is about 10^-15 so you could use h = 10^-7 or 10^-8.

For more details, see these notes on picking the step size for differential equations.

这篇关于实施C / C衍生物++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆