Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 243
fazan Avatar asked Jul 04 '26 15:07

fazan


1 Answers

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,))
like image 52
user2357112 supports Monica Avatar answered Jul 06 '26 21:07

user2357112 supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!