Reputation: 11
I'm trying to run a solver that uses multiple values in an np.array as constants. I want it to loop through them, but every time it goes through, it gives me the error: minpack.error: Result from function call is not a proper array of floats. Here's the code:
import numpy as np
from scipy.optimize import fsolve
G = np.linspace(8,12,3)
a = [5, 3, 7]
b = [10, 4, 5]
def equation1(xy, G):
ep, uc = xy
return(2*981*7.62*((ep**-4.7) - 1) - 0.01*(uc/ep - 399)**2,
(5.34*G - a) - (uc/ep - 399)*7.51*(1 - ep))
EP1 = []
UC1 = []
for i in range(0,len(G)):
ep1, uc1 = fsolve(equation1, (a[i], b[i]), G[i])
EP1.append(ep1)
UC1.append(uc1)
I've been trying to figure out where the problem is, and I think it has something to do with the section in the function's return where it says (5.34*G - a). If someone could help, I would really appreciate it. Thanks! By the way, the a and b numbers aren't realistic, so the function may not converge correctly.
Upvotes: 1
Views: 327
Reputation: 4547
In the return call of equation1
, you reference the variable a
which is out of scope. a
is actually a list, hence why minpack complains about the shape of the object you are trying to return. It looks to me that you simply forgot to swap this a
by ep
. Also, you should use root instead of fsolve, see here. This gives:
import numpy as np
from scipy.optimize import root
def func(xy, G):
ep, uc = xy
num = 2 * 981 * 7.62
return (num * (ep ** -4.7 - 1) - 0.01 * (uc / ep - 399) ** 2,
5.34 * G - ep - (uc / ep - 399) * 7.51 * (1 - ep))
G = np.linspace(8, 12, 3)
a = [5, 3, 7]
b = [10, 4, 5]
EP1 = []
UC1 = []
for i in range(G.size):
sol = root(func, [a[i], b[i]], args=G[i])
EP1.append(sol.x[0])
UC1.append(sol.x[1])
print(EP1, UC1)
Upvotes: 0