How to input another variable into scipy optimize function

Viewed 40

Please ignore the messiness, still a noob at this. Basically, I have a function:

def bw(Q,T):
  F = -Tc * Q**2 + T*((1+Q)*np.log(1+Q) + (1-Q)*np.log(1-Q))
  return F

And I need to find the minimum of this at various T's

Tc = 1
n = 200
T1 = 0.5
T2 = 1.2
Tt = np.linspace(T1,T2,n)
Qe = np.ndarray((1,n))
Q0 = 0.9

for i in range(n):
  T = Tt[i]
  res = minimize(bw,Q0,method='nelder-mead', options={'xatol': 1e-8})

With this, it says it's missing a positional argument T, but then:

res = minimize(bw(T),Q0,method='nelder-mead', options={'xatol': 1e-8})
res = minimize(bw(Q,T),Q0,method='nelder-mead', options={'xatol': 1e-8})
res = minimize(bw(,T),Q0,method='nelder-mead', options={'xatol': 1e-8})

I tried these and they don't work. Then, I tried:

res = minimize(bw,T,Q0,method='nelder-mead', options={'xatol': 1e-8})

The values I get are wrong. Can anyone help?

1 Answers

Your last attempt is correct, you just mixed up Q and T. It should be:

 res = minimize(bw,Q0,T,method='nelder-mead', options={'xatol': 1e-8})
Related