Python 不动点迭代算法
我被要求编写一个程序,用定点迭代法求解这个方程(x^3+x-1=0) 定点迭代的算法是什么? Python中是否有任何定点迭代代码示例?(不是来自任何模块的函数,而是带有算法的代码) 谢谢你伪代码是什么,你应该可以从那里找到它。首先,阅读以下内容: 我选择了牛顿的方法 现在,如果您想了解生成器函数,可以定义一个生成器函数,并按如下方式实例化生成器对象Python 不动点迭代算法,python,algorithm,numerical-analysis,fixed-point-iteration,Python,Algorithm,Numerical Analysis,Fixed Point Iteration,我被要求编写一个程序,用定点迭代法求解这个方程(x^3+x-1=0) 定点迭代的算法是什么? Python中是否有任何定点迭代代码示例?(不是来自任何模块的函数,而是带有算法的代码) 谢谢你伪代码是什么,你应该可以从那里找到它。首先,阅读以下内容: 我选择了牛顿的方法 现在,如果您想了解生成器函数,可以定义一个生成器函数,并按如下方式实例化生成器对象 def newtons_method(n): n = float(n) #Force float arithmetic nPl
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()
有关发电机的更多信息,请参阅:或
比如说
approx1 = approxAnswer.next()
approx2 = approxAnswer.next()
或者最好使用一个循环
至于决定什么时候你的近似值足够好…;) 到目前为止,您有什么经验?请先阅读以下内容:虽然很难理解,但实现起来很简单。