H X
H X

Reputation: 43

Scipy fmin argument passing

so I'm just a bit confused as to how scipy.optimize.fmin takes arguments

say I have two functions:

def func_1(x, y):
   return (x*x) + (y*y)

def func_2(x, y, a):
   return ((x-a)*(x-a)) + ((y-a) *(y-a))

in func_1 I simply want the min values for x and y, and in func_2 I'd ideally like to pass a value for a , and then find the min vals for x and y. I've read some of the other questions in regard to this, and am still a bit confused.

Attempts to call : fmin(fund_1, [1,1]) results in : fmin() takes at least 2 arguments (1 given)

Many thanks!

Upvotes: 3

Views: 8354

Answers (1)

gg349
gg349

Reputation: 22671

First, change the signature of func_1 and func_2 to something like:

def func_1(t):
   x,y=t
   return (x*x) + (y*y)

def func_2(t, a):
   x,y=t
   return ((x-a)*(x-a)) + ((y-a) *(y-a))

In the first case, use

from scipy.optimize import fmin
from numpy import array
fmin(func_1,array([1,1]))

In the second case, you must pass to fmin the argument args=(a,). It will do exactly what you describe,

fmin(func_2,array([1,1]),args=(3,))

Upvotes: 4

Related Questions