使用泰勒级数避免精度损失

发布于 2024-07-13 09:54:48 字数 502 浏览 12 评论 0原文

我正在尝试使用泰勒级数来开发一种数值上合理的算法来求解函数。 我已经做了很长一段时间了,但还没有任何运气。 我不确定我做错了什么。

该函数

f(x)=1 + x - sin(x)/ln(1+x)   x~0

还有:为什么该函数会出现精度损失? 当 x 接近于零时,sin(x)/ln(1+x) 甚至与 x 相差甚远。 我不明白在哪里失去了意义。

为了解决这个问题,我相信我需要使用 sin(x) 和 ln(1+x) 的泰勒展开式,它们分别是

x - x^3/3! + x^5/5! - x^7/7! + ...

x - x^2/2 + x^3/3 - x^4/4 + ...

。 我尝试使用类似的分母来组合 x 和 sin(x)/ln(1+x) 分量,甚至组合所有三个,但最终似乎没有任何结果正确。 任何帮助表示赞赏。

I'm trying to use Taylor series to develop a numerically sound algorithm for solving a function. I've been at it for quite a while, but haven't had any luck yet. I'm not sure what I'm doing wrong.

The function is

f(x)=1 + x - sin(x)/ln(1+x)   x~0

Also: why does loss of precision even occur in this function? when x is close to zero, sin(x)/ln(1+x) isn't even close to being the same number as x. I don't see where significance is even being lost.

In order to solve this, I believe that I will need to use the Taylor expansions for sin(x) and ln(1+x), which are

x - x^3/3! + x^5/5! - x^7/7! + ...

and

x - x^2/2 + x^3/3 - x^4/4 + ...

respectively. I have attempted to use like denominators to combine the x and sin(x)/ln(1+x) components, and even to combine all three, but nothing seems to work out correctly in the end. Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

晒暮凉 2024-07-20 09:54:48

可能会出现精度损失,因为当 x ~ 0 时,ln(1+x) 也接近 0,因此您最终会除以一个非常小的数字。 计算机不太擅长这一点;-)

如果您直接使用泰勒级数来表示 ln(1+x),这会有点痛苦,因为您最终会除以无限级数的项。 对于这样的情况,我通常更喜欢从定义中计算整个函数的泰勒级数:

f(x) = f(0) + f'(0) x + f''(0) x/2 + f'''(0) x/6 + ...

从中您将得到

f(x) = 2 + 3x/2 - x^2/4 - x^3/24 - x^4/240 - 23x^5/1440 + 31x^6/2880 ...

(我作弊并将其插入 Mathematica ;-)就像史蒂夫说的那样,这个级数不'尽管我目前想不出更好的方法,但收敛速度不会那么快。

编辑:我想我误解了这个问题 - 如果你想做的只是找到函数的零点,那么肯定有比使用泰勒级数更好的方法。

The loss of precision can come in because when x ~ 0, ln(1+x) is also close to 0, so you wind up dividing by a very small number. Computers aren't very good at that ;-)

If you use the Taylor series for ln(1+x) directly, it's going to be kind of a pain because you'll wind up dividing by an infinite series of terms. For cases like this, I usually prefer to just compute the Taylor series for the entire function as a whole from the definition:

f(x) = f(0) + f'(0) x + f''(0) x/2 + f'''(0) x/6 + ...

from which you'll get

f(x) = 2 + 3x/2 - x^2/4 - x^3/24 - x^4/240 - 23x^5/1440 + 31x^6/2880 ...

(I cheated and plugged it into Mathematica ;-) Like Steve says, this series doesn't converge all that quickly, although I can't think of a better method at the moment.

EDIT: I think I misread the question - if all you're trying to do is find the zeros of the function, there are definitely better ways than using a Taylor series.

陌上青苔 2024-07-20 09:54:48

由于这是家庭作业,我只是尝试给出一些正确方向的指导。

解决方案 1

不要使用 Talyor 级数近似,而是尝试简单地使用求根算法例如牛顿-拉夫森法、线性插值法或区间二分法(或将它们甚至组合起来)。 它们的实现非常简单,并且通过适当选择起始值,根可以很快收敛到精确值。

解决方案 2

如果您出于某种原因确实需要使用泰勒级数近似,那么只需展开 sin(x)、ln(x) 等即可。 (在您的情况下,乘以 ln(x) 可以删除分母)。 然后您需要使用某种多项式方程求解器。 如果您想要合理的准确度,则需要超出我想象的 3 次方或 4 次方,这意味着简单的分析解决方案并不容易。 但是,您可能需要研究类似于 Durand-Kerner 方法 的方法来解决一般问题任意阶多项式。 不过,如果您需要使用高阶项,这种方法只会导致复杂化,所以我肯定会推荐解决方案 1。

希望有所帮助...

As this is homework, I'm just going to try to give a few pointers in the right direction.

Solution 1

Rather than using the Talyor series approximation, try to simply use a root finding algorithm such as the Newton-Raphson method, linear interpolation, or interval bisection (or combine them even). They are very simple to implement, and with an appropiate choice of starting value(s), the root can converge to a precise value quite quickly.

Solution 2

If you really need to use the Taylor series approximation for whatever reason, then just expand the sin(x), ln(x), and whatever else. (Multiplying through by ln(x) to remove the denominator in your case will work). Then you'll need to use some sort of polynomial equation solver. If you want a reasonable degree of accuracy, you'll need to go beyond the 3rd or 4th powers I'd imagine, which means a simple analytical solution is not going to be easy. However, you may want to look into something like the Durand-Kerner method for solving general polynomials of any order. Still, if you need to use high-order terms this approach is just going to lead to complications, so I would definitely recommend solution 1.

Hope that helps...

心的位置 2024-07-20 09:54:48

我认为你需要看看当 x -->0 时 ln(x+1) 会发生什么,你就会明白为什么这个函数在 x = 0 附近表现不佳。

I think you need to look at what happens to ln(x+1) as x -->0 and you will see why this function does not behave well near x = 0.

丘比特射中我 2024-07-20 09:54:48

我还没有仔细研究过这个问题,但你应该知道一些泰勒级数收敛得非常非常慢。

I haven't looked into this that closely, but you should be aware that some taylor series converge very, very slowly.

感性 2024-07-20 09:54:48

只需直接计算 f 的泰勒级数即可。

Maxima 给了我(关于 x=0 的前 4 项):

(%i1) f(x):=1 + x - sin(x)/log(1+x);
                                           - sin(x)
(%o1)                     f(x) := 1 + x + ----------
                                          log(1 + x)


(%i2) taylor(f(x),x,0,4);
                                2    3    4
                           x   x    x    x
(%o2)/T/                   - + -- + -- + --- + . . .
                           2   4    24   240

Just compute the Taylor series of f directly.

Maxima gives me (first 4 terms about x=0):

(%i1) f(x):=1 + x - sin(x)/log(1+x);
                                           - sin(x)
(%o1)                     f(x) := 1 + x + ----------
                                          log(1 + x)


(%i2) taylor(f(x),x,0,4);
                                2    3    4
                           x   x    x    x
(%o2)/T/                   - + -- + -- + --- + . . .
                           2   4    24   240
谁的新欢旧爱 2024-07-20 09:54:48

问题中使用的方法是正确的 - 只需确保您的计算器处于弧度模式即可。

Method used in question is correct - just make sure your calculator is in radians mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文