定点迭代算法

时间:2010-12-02 01:49:57

标签: python algorithm fixed-point numerical-analysis

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

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

谢谢

2 个答案:

答案 0 :(得分:1)

首先,请阅读: Fixed point iteration:Applications

我选择了牛顿方法。

现在,如果您想了解生成器函数,可以定义生成器函数,并按如下方式实例生成器对象

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 255Classes (Generators) - Python v2.7了解有关发电机的更多信息

例如

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

或者更好的是使用循环!

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

答案 1 :(得分:0)

伪代码是here,你应该可以从那里弄明白。

相关问题