Skip to content Skip to sidebar Skip to footer

Unable To Interprete Matlab Interp2d In Python Scipy.interp

The following code is just to understand the context. My question doesn't require much understanding of this. It need a simple translation of one line of MATLAB code in to Python.

Solution 1:

The problem on a wide scale is that you're looking at the documentation but you're not trying to understand it. If you're using a specific function interp2d in the module scipy.interpolate, then look at the function's documentation, as @pv also suggested in a comment. The fact that you're throwing the arguments all around the place clearly demonstrates that you're trying to use a function based on guesswork. It won't work: a function was implemented with a given syntax, and it will only work like that.

So look at the function's signature:

class scipy.interpolate.interp2d(x, y, z, kind='linear', copy=True, bounds_error=False, fill_value=nan)

The meaning of the parameters is explained afterwards. You can clearly see that there are 3 mandatory parameters: x, y, z. No other array-valued inputs are allowed This is because interp2d only constructs an interpolating function, which you should then use to compute the interpolated values on a mesh (unlike MATLAB, where interp2d gives you the interpolated values directly). So you can call

myfun = interp2(uu,vv,proj,'linear')

to get an interpolating function. You can then substitute at the given values, but note that the function myfun will expect 1d input, and it will construct a mesh internally. So assuming that your output mesh is constructed as

puu,puv = np.meshgrid(puu_vec,puv_vec)

(which is probably not the case, but I'll get back to this later), you need

vol[:,:,iz] = Ratio*myfun(puu_vec,puv_vec)

to obtain the output you need.

However, there are some important points you should note.

  1. In MATLAB you have interp2d(uu,vv,proj',...), but make sure that for scipy the elements of uu, vv, and proj are in the same order. To be on the safe side: in case of an asymmetric mesh size, the shape of uu, vv and proj should all be the same.
  2. In MATLAB you're using 'linear' interpolation, while in python you're using 'cubic'. I'm not sure this is really what you want, if you're porting your code to a new language.
  3. It seems to me that your output mesh is not defined by a rectangular grid as if from meshgrid, which suggests that interp2d might not be suitable for your case. Anyway, I've had some odd experiences with interp2d, and I wouldn't trust it. So if it's not suitable for your expected output, I'd strongly suggest using scipy.interpolate.griddata instead. This function gives you interpolated points directly: I suggest that you try to figure out its use based on the manual:) My linked answer can also help. You can set the kind of interpolation in the same way, but your output can be any set of scattered points if you like.

Post a Comment for "Unable To Interprete Matlab Interp2d In Python Scipy.interp"