pythonscipyscipy-optimizebisectionmultiple-return-values

Python: Can scipy.optimize,bisection use a function f that returns multiple values?


Suppose I have a function, f, that returns multiple values.

def f(x):
    return x + 1, 2 * x

Scipy documentation tells me that f must be a function that returns a number. I take it that my given function f isn't compatible for scipy.optimize.bisection, but is it possible to perform the bisection method on just one of the return values of f?


Solution

  • You can define a function that calls your function f and extracts the second return value:

    from scipy.optimize import bisect
    
    def f(x):
        return x + 1, 2 * x
    
    bisect(lambda x: f(x)[1], -1, 1)