Margaral
Margaral

Reputation: 27

Using fsolve with scipy function

I have encountered the following problem with scipy.fsolve, but I don't what to do:

U = 0.00043
ThC =1.19
Dist = 7   
IncT = 0.2  
pcw = 1180000          
k = 1.19                    
B = U * pcw / (2 * k)

fugato = fsolve((((Ql/(2*math.pi* k))*math.exp(B * x)*special.kv(0, B * x))-IncT),0.01)

print fugato

I get the error TypeError: 'numpy.float64' object is not callable in fsolve.

How do I fix this problem?

Upvotes: 1

Views: 4285

Answers (1)

Pascal Bugnion
Pascal Bugnion

Reputation: 4928

The argument to fsolve must be a function.

I presume that you want to solve your equation for x? If so, writing:

fugato = fsolve(lambda x: Ql/(2*math.pi* k)*math.exp(B * x)*special.kv(0, B * x)-IncT,
                0.01)

works.


To explain what's going on here, the construct lambda x: 2*x is a function definition. It is similar to writing:

def f(x):
    return 2*x

The lambda construction is commonly used to define functions that you only need once. This is often the case when registering callbacks, or to represent a mathematical expression. For instance, if you wanted to integrate f(x) = 2*x, you could write:

from scipy.integrate import quad
integral = quad(lambda x: 2*x, 0., 3.)

Similarly, if you want to solve 2*x = 1, you can write:

from scipy.optimize import fsolve
fsolve(lambda x: 2*x-1, 0.)

Upvotes: 2

Related Questions