The bisection method

本文深入探讨了二分法求解方程根的基本原理与实现过程,通过具体实例展示了如何利用二分法找到方程的近似解,并提供了一段Python代码实现,帮助读者理解和掌握这一数值分析中的经典算法。
"""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)
Returns ------- res : OptimizeResult The optimization result represented as a ``OptimizeResult`` object. Important attributes are: ``x`` the solution array, ``success`` a Boolean flag indicating if the optimizer exited successfully and ``message`` which describes the cause of the termination. See `OptimizeResult` for a description of other attributes. See also -------- minimize : Interface to minimization algorithms for scalar multivariate functions show_options : Additional options accepted by the solvers Notes ----- This section describes the available solvers that can be selected by the 'method' parameter. The default method is the ``"Bounded"`` Brent method if `bounds` are passed and unbounded ``"Brent"`` otherwise. Method :ref:`Brent <optimize.minimize_scalar-brent>` uses Brent's algorithm [1]_ to find a local minimum. The algorithm uses inverse parabolic interpolation when possible to speed up convergence of the golden section method. Method :ref:`Golden <optimize.minimize_scalar-golden>` uses the golden section search technique [1]_. It uses analog of the bisection method to decrease the bracketed interval. It is usually preferable to use the *Brent* method. Method :ref:`Bounded <optimize.minimize_scalar-bounded>` can perform bounded minimization [2]_ [3]_. It uses the Brent method to find a local minimum in the interval x1 < xopt < x2. Note that the Brent and Golden methods do not guarantee success unless a valid ``bracket`` triple is provided. If a three-point bracket cannot be found, consider `scipy.optimize.minimize`. Also, all methods are intended only for local minimization. When the function of interest has more than one local minimum, consider :ref:`global_optimization`. **Custom minimizers** It may be useful to pass a custom minimization method, for example when using some library frontend to minimize_scalar. You can simply pass a callable as the ``method`` parameter. The callable is called as ``method(fun, args, **kwargs, **options)`` where ``kwargs`` corresponds to any other parameters passed to `minimize` (such as `bracket`, `tol`, etc.), except the `options` dict, which has its contents also passed as `method` parameters pair by pair. The method shall return an `OptimizeResult` object. The provided `method` callable must be able to accept (and possibly ignore) arbitrary parameters; the set of parameters accepted by `minimize` may expand in future versions and then these parameters will be passed to the method. You can find an example in the scipy.optimize tutorial. .. versionadded:: 0.11.0
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值