An incremental_search algorithm

本文介绍了一种用于求解函数根的增量搜索算法,通过在指定区间内逐步增加步长进行搜索,直至找到根的近似位置。算法详细描述了如何在函数符号变化处定位根,并提供了迭代次数作为算法效率的衡量标准。
"""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)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值