国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python計(jì)算方程式根的方法

2020-02-23 01:08:35
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了python計(jì)算方程式根的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

''' 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,complexfrom cmath import sqrtfrom random import randomdef 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) < tol: return x      g = dp/p      h = g*g - ddp/p      f = sqrt((n - 1)*(n*h - g*g))      if abs(g + f) > abs(g - f): dx = n/(g + f)      else: dx = n/(g - f)      x = x - dx      if abs(dx) < tol: return x    print 'Too many iterations'  def deflPoly(a,root): # Deflates a polynomial    n = len(a)-1    b = [(0.0 + 0.0j)]*n    b[n-1] = a[n]    for i in range(n-2,-1,-1):      b[i] = a[i+1] + root*b[i+1]    return b  n = len(a) - 1  roots = zeros((n),dtype=complex)  for i in range(n):    x = laguerre(a,tol)    if abs(x.imag) < tol: x = x.real    roots[i] = x    a = deflPoly(a,x)  return roots  raw_input("/nPress return to exit")

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 拉孜县| 西充县| 洮南市| 香格里拉县| 海淀区| 海安县| 新宁县| 扶风县| 大洼县| 江达县| 湟源县| 汶川县| 武宣县| 林州市| 池州市| 沧州市| 清涧县| 闽清县| 天峨县| 凤城市| 那坡县| 桐梓县| 苗栗县| 斗六市| 洛浦县| 石河子市| 金阳县| 游戏| 叶城县| 济阳县| 故城县| 东乡族自治县| 铁力市| 武安市| 三都| 宜兰市| 隆子县| 镇平县| 民县| 宜昌市| 桂阳县|