Python查找函數(shù)f(x)=0根的解決方法
更新時間:2015年05月07日 11:28:51 作者:songguo
這篇文章主要介紹了Python查找函數(shù)f(x)=0根的解決方法,涉及Python數(shù)學(xué)運(yùn)算函數(shù)求解的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了Python查找函數(shù)f(x)=0根的解決方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
''' root = ridder(f,a,b,tol=1.0e-9).
Finds a root of f(x) = 0 with Ridder's method.
The root must be bracketed in (a,b).
'''
import error
from math import sqrt
def ridder(f,a,b,tol=1.0e-9):
fa = f(a)
if fa == 0.0: return a
fb = f(b)
if fb == 0.0: return b
if fa*fb > 0.0: error.err('Root is not bracketed')
for i in range(30):
# Compute the improved root x from Ridder's formula
c = 0.5*(a + b); fc = f(c)
s = sqrt(fc**2 - fa*fb)
if s == 0.0: return None
dx = (c - a)*fc/s
if (fa - fb) < 0.0: dx = -dx
x = c + dx; fx = f(x)
# Test for convergence
if i > 0:
if abs(x - xOld) < tol*max(abs(x),1.0): return x
xOld = x
# Re-bracket the root as tightly as possible
if fc*fx > 0.0:
if fa*fx < 0.0: b = x; fb = fx
else: a = x; fa = fx
else:
a = c; b = x; fa = fc; fb = fx
return None
print 'Too many iterations'
希望本文所述對大家的Python程序設(shè)計有所幫助。
您可能感興趣的文章:
相關(guān)文章
淺談哪個Python庫才最適合做數(shù)據(jù)可視化
數(shù)據(jù)可視化是任何探索性數(shù)據(jù)分析或報告的關(guān)鍵步驟,目前有許多非常好的商業(yè)智能工具,比如Tableau、googledatastudio和PowerBI等,本文就詳細(xì)的進(jìn)行對比,感興趣的可以了解一下2021-06-06
Python腳本,標(biāo)識符,變量使用,腳本語句,注釋,模塊引用詳解
這篇文章主要為大家詳細(xì)介紹了Python腳本,標(biāo)識符,變量使用,腳本語句,注釋,模塊引用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02

