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//
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With