Akash
Akash

Reputation: 3

Error: Result from function call is not a proper array of floats

Getting error messages :

ValueError: setting an array element with a sequence.
error: Result from function call is not a proper array of floats.

For this code:

def f(z):
    z0 = z[0]
    z1 = z[1]
    z2 = z[2]
    z3 = z[3]
    z4 = z[4]
    z5 = z[5]
    z6 = z[6]

    f1 = z0 - const1,          # Q_e = m_w*(hw_in - hw_out)
    f2 = z1 - z0*const2,       # m_o = Q_e/(h_5 - h_2)
    f3 = z2 - z1*const3,       # W_T = m_o*(h_5 - h6_s)*n_T
    f4 = z3 - z1*const4,       # W_P = (m_o*(h2_s - h_1))/n_P
    f5 = z4 - z5*const5,       # W_cool = m_cool*g*H
    f6 = z5 - z1*const6        # m_cool = (m_o*(h_7 - h_1))/(hcool_pp - hcool_in)
    f7 = z6 - z2 + z3 + z4     # W_net = W_T - W_P - W_cool

    return [f1,f2,f3,f4,f5,f6,f7]

x = opt.fsolve(f,[100,100,100,100,100,100,100])

Is there any problem with the initial guesses or using fsolve? Shouldn't there be a different type of error message for wrong initial guess?
I have also used instead:

   return np.array([f1,f2,f3,f4,f5,f6,f7])
x = opt.fsolve(f,np.array([100,100,100,100,100,100,100])

but then there comes:

TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'
and
error: Result from function call is not a proper array of floats.

Upvotes: 0

Views: 1584

Answers (1)

bhaskarc
bhaskarc

Reputation: 9541

Your problem is that you have added comma after every f1 to f7 calculations.

f2 = z1 - z0*const2, <-- remove these commas

The trailing comma converts that number to a tuple and hence every thing goes haywire.

Remove all trailing commas and it should work as expected.

Here's your modified example:

from scipy.optimize import fsolve

def f(z):
    z0 = z[0]
    z1 = z[1]
    z2 = z[2]
    z3 = z[3]
    z4 = z[4]
    z5 = z[5]
    z6 = z[6]
    f1 = z0 - 2         # Q_e = m_w*(hw_in - hw_out)
    f2 = z1 - z0*3       # m_o = Q_e/(h_5 - h_2)
    f3 = z2 - z1*3       # W_T = m_o*(h_5 - h6_s)*n_T
    f4 = z3 - z1*4       # W_P = (m_o*(h2_s - h_1))/n_P
    f5 = z4 - z5*5       # W_cool = m_cool*g*H
    f6 = z5 - z1*6        # m_cool = (m_o*(h_7 - h_1))/(hcool_pp - hcool_in)
    f7 = z6 - z2 + z3 + z4     # W_net = W_T - W_P - W_cool
    return [f1,f2,f3,f4,f5,f6,f7]


arr = [100,100,100,100,100,100,100]
x = fsolve(f,arr)
print(x)

This prints [ 2. 6. 18. 24. 180. 36. -186.]

Upvotes: 0

Related Questions