"""An incremental search algorithm"""
import numpy as np
def incremental_search(f,a,b,dx):
"""
f:the function to sovle
a:the left boundary x-axis value
b:the right boundary x-axis value
dx:the invreamental value in searching
return:the x-axis value of the root,
number of interations used
"""
fa = f(a)
c = a +dx
fc = f(c)
n = 1
while np.sign(fa) == np.sign(fc):
if a >= b:
return a - dx,n
a = c
fa = fc
c = a+dx
fc = f(c)
n+=1
if fa == 0:
return a,n
elif fc == 0:
return c,n
else:
return (a+c)/2.,n
"""
For example
"""
y = lambda x:x**3 +2.0*x**2-5
root,interations = incremental_search(y,-5.,5.,0.001)
print("root is",root)
print("interations:",interations)
An incremental_search algorithm
最新推荐文章于 2024-10-31 14:44:51 发布
本文介绍了一种用于求解函数根的增量搜索算法,通过在指定区间内逐步增加步长进行搜索,直至找到根的近似位置。算法详细描述了如何在函数符号变化处定位根,并提供了迭代次数作为算法效率的衡量标准。
497

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



