2010-12-02 67 views
0

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

定点迭代算法是什么? Python中是否有任何固定点迭代代码示例? (而不是从任何模块的功能,但与算法的代码)

谢谢

+4

到目前为止你有什么? – SingleNegationElimination 2010-12-02 01:53:07

回答

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() 

或更好的使用循环!

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

0

伪代码here,你应该能够从那里弄明白。