定点迭代算法

发布于 2024-10-06 02:10:28 字数 123 浏览 0 评论 0原文

我被要求编写一个程序来使用定点迭代求解这个方程( x^3 + x -1 = 0 )。

定点迭代的算法是什么? Python 中有定点迭代代码示例吗? (不是任何模块的函数,而是带有算法的代码)

谢谢

I am asked to write a program to solve this equation ( x^3 + x -1 = 0 ) using fixed point iteration.

What is the algorithm for fixed point iteration?
Is there any fixed point iteration code sample in Python? (not a function from any modules, but the code with algorithms)

Thank You

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

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

发布评论

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

评论(2

若言繁花未落 2024-10-13 02:10:28

首先,阅读以下内容:
定点迭代:应用

我选择了牛顿法。

现在,如果您想了解生成器函数,您可以定义一个生成器函数,并实例化一个生成器对象,如下所示

def newtons_method(n):
    n = float(n)  #Force float arithmetic
    nPlusOne = n - (pow(n,3) + n - 1)/(3*pow(n,2) +1)
    while 1:
        yield nPlusOne
        n = nPlusOne
        nPlusOne = n - (pow(n,3) + n - 1)/(3*pow(n,2) +1)

approxAnswer = newtons_method(1.0)   #1.0 can be any initial guess...

然后您可以通过调用连续获得更好的近似值:

approxAnswer.next()

请参阅: PEP 255类(生成器)- Python v2.7 有关生成器的更多信息

例如

approx1 = approxAnswer.next()
approx2 = approxAnswer.next()

,或者更好的是使用循环!

至于决定你的近似值何时足够好......;)

First, read this:
Fixed point iteration:Applications

I chose Newton's Method.

Now if you'd like to learn about generator functions, you could define a generator function, and instance a generator object as follows

def newtons_method(n):
    n = float(n)  #Force float arithmetic
    nPlusOne = n - (pow(n,3) + n - 1)/(3*pow(n,2) +1)
    while 1:
        yield nPlusOne
        n = nPlusOne
        nPlusOne = n - (pow(n,3) + n - 1)/(3*pow(n,2) +1)

approxAnswer = newtons_method(1.0)   #1.0 can be any initial guess...

Then you can gain successively better approximations by calling:

approxAnswer.next()

see: PEP 255 or Classes (Generators) - Python v2.7 for more info on Generators

For example

approx1 = approxAnswer.next()
approx2 = approxAnswer.next()

Or better yet use a loop!

As for deciding when your approximation is good enough... ;)

缱倦旧时光 2024-10-13 02:10:28

伪代码是这里,你应该能够理解它从那里出来。

Pseudocode is here, you should be able to figure it out from there.

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