"""The bisection method"""
def bisection(f,a,b,tol=0.1,maxiter=10):
"""
f:the function to solve
a:the x-axis value where f(a)<0
b:the x-axis value where f(b)>0
tol:the precision of the solution
maxiter:max number of interations
return:the x-axis value of the root,
number of interations used
"""
c = (a+b)/2
n=1
while n < maxiter:
c = (a+b)/2
if f(c) == 0 or abs(a-b)*0.5 < tol:
return c,n
n+=1
if f(c) <0:
a = c
else:
b = c
return c,n
"""
for example
"""
y = lambda x:x**3 +2.0*x**2-5
root,interations = bisection(y,-5,5,0.00001,100)
print("root is:",root)
print("interations is:",interations)
The bisection method
最新推荐文章于 2025-12-05 08:37:05 发布
本文深入探讨了二分法求解方程根的基本原理与实现过程,通过具体实例展示了如何利用二分法找到方程的近似解,并提供了一段Python代码实现,帮助读者理解和掌握这一数值分析中的经典算法。
7670

被折叠的 条评论
为什么被折叠?



