国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 崇礼县| 沛县| 桃源县| 平南县| 千阳县| 关岭| 安泽县| 中西区| 南郑县| 延寿县| 菏泽市| 利辛县| 镇平县| 丘北县| 株洲市| 宁强县| 祁门县| 襄汾县| 高青县| 达尔| 绵竹市| 湖州市| 称多县| 镶黄旗| 封开县| 读书| 虎林市| 焦作市| 碌曲县| 海晏县| 保靖县| 五指山市| 丰原市| 巴马| 通化市| 蓝田县| 兴宁市| 孟州市| 霞浦县| 溆浦县| 上蔡县|