Find a root of a function in a given range

前端 未结 3 1580
感动是毒
感动是毒 2021-01-19 15:35

I have a set of functions f_t with several roots (two actually). I want to find the \"first\" root and doing this with fsolve works fine most of th

3条回答
  •  逝去的感伤
    2021-01-19 16:01

    It is generally accepted that for smooth, well-behaved functions, the Brent method is the fastest method guaranteed to give a root. As with the other two methods listed, you must provide an interval [a,b] across which the function is continuous and changes sign.

    The Scipy implementation is documented here. An example use case for the function you mentioned could look like this:

    from __future__ import division
    import scipy
    
    def func(x,t):
        return(x**2 - 1/t)
    
    t0 = 1
    min = 0
    max = 100000 # set max to some sufficiently large value
    
    root = scipy.optimize.brentq(func, min, max, args = (t0)) # args just supplies any extra
                                                           # argument for the function that isn't the varied parameter
    

提交回复
热议问题