I have a function as below:
def fun_root(x, *pars):
a, b, fsolve = pars
exp1 = x**a - b*x + 2
exp2 = np.exp(a*x) + x**b
if fsolve == 1:
return exp1-exp2
elif fsolve == 0:
return exp2
And I use the following code to find the value of exp2 with fsolve
given root.:
tuple1 = (2, 3)
tuple2 = tuple1 + (1,)
tuple3 = tuple1 + (0,)
result_x = scipy.optimize.fsolve(fun_root, np.array((1)), tuple2)
print(result_x)
result_exp2 = fun_root(result_x, tuple3)
print(result_exp2)
I can get one root, which is 0.189. However, I get an error message regarding the line before the last:
a, b, fsolve = pars
ValueError: not enough values to unpack (expected 3, got 1)
What is the problem in the code above?
Ps. I use optional returns in the function since in my real case the function is complicated and I cannot get an explicit expression of exp2.
In this line
you are passing
tuple2
as theargs
parameter offsolve
. The functionfsolve
will unpack that tuple for you when it callsfun_root
.In this line
you are passing
tuple3
(a single python object that happens to be a tuple) tofun_root
. In this case,args
will be((2, 3, 0),)
. That is, it will be tuple of length 1, containing the tuple that you passed in. Based on the line that causes the error, it is clear that what you want to do is unpacktuple3
in the call offun_root
, so the line should beA less elegant way to accomplish the same result is