Search code examples
pythonpython-3.xscipybisect

How to use scipy.optimize.bisect() when function has additional parameters?


According to the documentation, I should be able to bisect a function with multiple parameters as long as I pass said parameters to bisect() using args=(). However, I just can't make it work and I didn't manage to find an example of using this function in such a scenario.

My function is of shape $f(a,x)$, where the user inputs $a$ and the program finds a root in variable x using scipy.optimize.bisect().

I tried passing it as:

scipy.optimize.bisect(f,-a,a,args=(a,))

But that didn't exactly work.


Solution

  • The args arguments are added after the argument the root finding operates on, not before. If you want to perform root-finding on the last argument instead of the first, you'll need to write a wrapper function that adapts your function's signature to what bisect expects.

    def g(x, a):
        return f(a, x)
    
    do_whatever_with(scipy.optimize.bisect(g, -a, a, args=(a,))