0

I am trying to convert a matlab code into Python and need a help with fsolve function. The Matlab function is of form

{[beta0,val,exitflag] = fsolve(@(beta) solve_obj(beta,y,x,z,z1), tb);}

where, y,x,z and z1 are given arguments and function need to solve for "beta" with tb as the initial guess. y,x are dataframes and z and z1 are boolean arrays.

I am trying to find appropriate replacement in python but I am unable to figure out how to pass the arguments as an input. I tried to use below function.

{from scipy.optimize import fsolve beta0 = fsolve(solve_obj, tb, (y,x,z,z1))}

but it gives error that "solve_obj() takes 2 positional arguments but 5 were given"

Can you please suggest how the arguments in form of Dataframe and array can be passed for fsolve in Python

  • Looks like you need to post more information. `solve_obj` should be "A function that takes at least one (possibly vector) argument, and returns a value of the same length" and is of the format `f(x, *args)`. The way you have your call structured your are passing x (with first guess `tb`), followed by four arguments `(y,x,z,z1)`. If you want those passed as a single vector, pass them as `([y,x,z,z1])`. https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html – Thursdays Coming Jun 05 '18 at 01:46
  • thanks a lot, I figured out a mistake in the code. I wasn't passing the argument as *args and I was just passing the list. Thanks for the help. – chaoticjoker Jun 07 '18 at 23:30

0 Answers0