3

I have a problem I can't figure out. I have created a interpolated function from data using scipy.interpolate.interp2d this gives me a callable function of two variables

 def Time_function():

      '''creates a interpolated function of Time depending on
      Cl and Cd using the in and outputs of matlab sim run.
     '''

      return interp2d(import_data()[0], import_data()[1], import_data()[2])

Witch works well, however I now want to find the minimum of this function using scipy.optimize.fmin or mimimize

def find_min_time():

     '''finds the min time based on the interpolated function
     '''

     f = Time_function()

     return minimize(f, np.array([1.0, 0.4]))

f obviously takes 2 arguments so minimize will need a function (f) and two guesses. However I can't seem to find the correct way to input the initail guesses as I get this error:

TypeError: __call__() takes at least 3 arguments (2 given)

Anyone know of a solution?

Cheers//

user5786425
  • 33
  • 1
  • 3
  • I don't see where **f** takes 2 arguments; I see no explicit parameters. Also, I suspect that your problem is that you pass the _value_ of a Time_function call, rather than the function itself. Try **minimize(Time_function, [1.0, 0.4])** ? – Prune Jan 13 '16 at 20:35
  • That returns TypeError: Time_function() takes no arguments (1 given) , wich is strange – user5786425 Jan 13 '16 at 21:41
  • Rats. Well, if you'd care to supply a [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve), I'll be happy to work on the mechanics. – Prune Jan 13 '16 at 22:39

1 Answers1

3

From the help of scipy.interpolate.interp2d:

 |  __call__(self, x, y, dx=0, dy=0, assume_sorted=False)
 |      Interpolate the function.
 |      
 |      Parameters
 |      ----------
 |      x : 1D array
 |          x-coordinates of the mesh on which to interpolate.
 |      y : 1D array
 |          y-coordinates of the mesh on which to interpolate.

From the help of scipy.optimize.minimize:

minimize(fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None)
    Minimization of scalar function of one or more variables.
    
    ....
    
    Parameters
    ----------
    fun : callable
        Objective function.
    x0 : ndarray
        Initial guess.

So it seems that interp2d constructs a function with 2 separate input parameters, but minimize tries to stuff in both variables as two components of the same input ndarray. You can use a lambda to mediate between the two syntaxes:

f = Time_function()

return minimize(lambda v: f(v[0],v[1]), np.array([1.0, 0.4]))

On a side note, I've found interp2d to give weird results sometimes. You might want to consider using scipy.interpolate.griddata, which doesn't construct an interpolating function for you, but rather computes substituted values for given input points (but you can access the interpolating functions themselves, such as LinearNDInterpolator). While I'd expect griddata to do a better job at interpolating, it would possibly (probably) be slower than substituting into a single interpolating function constructed with interp2d (but I'd check regardless).

  • That did it! However as you say the interp2d does not seem to me very accurate so I'll try it using griddata! Thanks alot! – user5786425 Jan 13 '16 at 23:33
  • @user5786425 I'm glad I could help. I also find it probable that using `LinearNDInterpolator` will be quicker than many calls to `griddata`, you should check both. Of course if you're not using bilinear interpolation, then another interpolating back-end should be used. – Andras Deak -- Слава Україні Jan 13 '16 at 23:42