问题描述
我目前正在编写一个计算器应用程序。我试图在其中写一个导数估计器。下面的公式是一种简单的方法。通常,在纸上,您将使用尽可能小的h来获得最准确的估计。问题是,双打无法处理将较小的数字加到相对较大的数字。例如4 + 1E-200只会得到4.0。即使h只是1E-16,实际上4 + 1E16也会为您提供正确的值,但进行数学计算却是不准确的,因为第16位之后的所有内容都会丢失,并且舍入不能正确进行。我听说双打的一般经验法则是1E-8或1E-7。问题在于大量数字将无法工作,因为2E231 + 1E-8只会是2E23,而1E-8会因为尺寸问题而丢失。
I am currently writing a calculator application. I am trying to write a derivative estimator into it. The formula below is a simple way to do it. Normally on paper you would use the smallest h possible to get the most accurate estimate. The problem is doubles can't handle adding really small numbers to comparatively huge numbers. Such as 4+1E-200 will just result in 4.0. Even if h was just 1E-16, 4+1E16 will in fact give you the right value but doing math it it is inaccurate because anything after the 16th place is lost and rounding can't happen correctly. I have heard the general rule of thumb for doubles is 1E-8 or 1E-7. The issue with this is large numbers wont work as 2E231+1E-8 will just be 2E23, the 1E-8 will be lost because of size issues.
f'(x)=(f(x + h)-f(x))/ h,当x接近0
当我在点4这样f'(4)上测试f(x)= x ^ 2时,它应该恰好是8
现在我知道我可能永远也不会精确得到8。但是我最准确的似乎是有趣的是1E-9或1E-8或1E8
都与1E-11给出相同的答案。
这是 f(x)= x ^ 2在x = 4处的h和结果的列表
When I test f(x)=x^2 at the point 4 so f'(4), it should be exactly 8now I understand that I will probably never get exactly 8. but I the most accurate seems to be around 1E-7 or 1E8the funny thing is 1E-9 all the to 1E-11 give the same answer.Here is a list of h's and results for f(x)=x^2 at x=4
1E-7 8.000000129015916
1E-8 7.999999951380232
1E-9 8.000000661922968
1E-10 8.000000661922968
1E-11 8.000000661922968
1E-12 8.000711204658728
这是我的问题:
- 选择h的最佳方法是什么,显然1E-8或1E-7是有意义的,但是我如何基于x来选择h,以便它适用于任何大小的数字,即使x是3.14E203或2E-231。
- 我应该占几位小数精度。
- 您有什么想法吗德州仪器(TI),TI 83、84和Inspire如何做到这一点,可以通过数值计算出导数到12位小数或精度,并且几乎总是正确的,但是无论如何,其数字的最大精度是12位,并且那些计算器不是CAS,所以它们不是实际上并没有得到任何东西
- 逻辑上在1E-7和1E-8之间有一个数字会给我一个更精确的结果,是否有任何方法可以找到该数字,或者至少可以接近该数字。
- What is the best way to pick h, obviously 1E-8 or 1E-7 make sense but how can I pick an h based off of x, so that it will work with any sized number even if x is 3.14E203 or 2E-231.
- How many decimals of precision should I account for.
- Do you have any idea how Texas instruments does it, the TI 83, 84, and Inspire can numerically figure out derivatives to 12 decimals or precision and almost always be right, but the maximum precision of their numbers is 12 digits anyway and those calculators are non CAS so they aren’t actually deriving anything
- Logically there is a number somewhere between 1E-7 and 1E-8 that will give me a more precise result, is there any way to find that number, or at least get close to it.
ANSWERED
非常感谢BobG。目前,该应用程序计划采用两种形式,即命令行PC应用程序。还有一个Android应用程序。特别感谢您的关于页面部分。如果您希望它是开源的,但是在我解决一些非常大的错误之前,我不会发布指向项目站点的链接。目前我一直称其为Mathulator,但名称可能会更改,因为它已经拥有版权并且听起来很愚蠢。我不知道什么时候发布候选版本会运行,目前我不知道何时发布会稳定的。但是如果我也能实现我想要的一切,它将非常强大。再次感谢。
Thank you very much BobG. The application is currently planned to be in 2 forms, a command line PC application . And an Android application. You will be mentioned in the special thanks to portions of the About page. If you would like it will be open source but I am not posting links to the project site until I work out some very very large bugs. At the moment I have been calling it Mathulator, but the name will likely change because there is already a copyright on it and it sounds stupid.I have no clue when the release candidate will be running, at the moment I have no clue when it will be stable. But it will be very powerful if I can implement everything I want too.Thanks again. Happy Programming.
推荐答案
有一本书可以回答这个问题(和其他类似的问题):
There's a book that answers this question (and others like it):
数字食谱C ,第二版,由Press,Vetterling,Teukolsky和Flannery撰写。本书还提供C ++,Fortran和BASIC版本。可悲的是,不存在Java版本。此外,我认为这本书已绝版,但可以在线上购买某些口味的二手版本(至少通过bn.com。)
Numerical Recipes in C, 2nd Edition, by Press, Vetterling, Teukolsky, and Flannery. This book also comes in C++, Fortran, and BASIC versions, as well. Sadly, no Java version exists. Additionally, I believe this book is out of print, but it is possible to buy used versions of some of the flavors online (at least through bn.com.)
部分5.7,数值导数,p。 186精确地解释了您在使用数值导数时遇到的问题及其发生原因的数学原理,以及用于正确计算数值导数的函数(在C语言中,但应易于转换为Java)。此处给出了它们的简单近似值的摘要:
Section 5.7, "Numerical Derivatives," p. 186 explains exactly the problem you're seeing with numerical derivatives and the math behind why it happens, along with a function for how to compute a numerical derivative properly (in C, but it should be easy to translate to Java). A summary of their simple approximation is presented here:
1)从数字上讲,最好计算对称版本:
1) Numerically, you're better off computing the symmetric version:
f'(x)=(f(x + h)-f(x-h))/ 2h
f'(x) = (f(x + h) - f(x - h)) / 2h
2)h应约为(sigma_f )^(1/3)* x_c
2) h should be approximately (sigma_f)^(1/3) * x_c
其中
sigma_f =〜f计算的分数精度(x)用于简单函数
sigma_f =~ fractional accuracy of the computation of f(x) for simple functions
x_c =〜x,除非x等于零。
x_c =~ x, unless x is equal to zero.
但是,这不会导致最优导数,因为误差为〜(sigma_f)^(2/3)。更好的解决方案是Ridders算法,该算法在本书中以C程序的形式呈现(参见Ridders,CJF 1982,Advances in Engineering Software,第4卷,第2期,第75-76页。)
However, this does not result in optimal derivatives, as the error is ~ (sigma_f)^(2/3). A better solution is Ridders' algorithm, which is presented as the C program in the book (ref. Ridders, C.J.F. 1982, Advances in Engineering Software, vol. 4, no. 2, pp 75-76.)
这篇关于Java估计一个点的导数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!