好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

python计算方程式根的方法

本文实例讲述了python计算方程式根的方法。分享给大家供大家参考。具体实现方法如下:

''' roots = polyRoots(a).
  Uses Laguerre's method to compute all the roots of
  a[0] + a[1]*x + a[2]*x^2 +...+ a[n]*x^n = 0.
  The roots are returned in the array 'roots',
'''  
from evalPoly import *
from numpy import zeros,complex
from cmath import sqrt
from random import random
def polyRoots(a,tol=1.0e-12):
  def laguerre(a,tol):
    x = random()
    # Starting value (random number)
    n = len(a) - 1
    for i in range(30):
      p,dp,ddp = evalPoly(a,x)
      if abs(p)   abs(g - f): dx = n/(g + f)
      else: dx = n/(g - f)
      x = x - dx
      if abs(dx)  

希望本文所述对大家的Python程序设计有所帮助。

查看更多关于python计算方程式根的方法的详细内容...

  阅读:39次