# Creating an Interpolation of a w = f(x,y,z) function

I am trying to finish a series of interpolation functions. The problem is more related with organizing the data than how to do the interpolations.

Using the RegularGridInterpolator, I created this function:

def interp_3d(x,y,z,fxyz,x_desired,y_desired,z_desired):
interp_func = RegularGridInterpolator((x,y,z), fxyz)
return interp_func([x_desired,y_desired,z_desired])


It gets three one dimensional arrays (x,y,z) and the function answer (f(x,y,z)) to return the interpolating function. The problem I am having is related to the format the data is given to me and how the function receives it. The data comes to me as a table:

x   y   z   f(x,y,z)
26600   5000    0.05    0.01
26600   5000    0.10    0.02
26600   5000    0.15    0.03
26600   5000    0.20    0.04
26600   5000    0.25    0.05
26600   10000   0.05    0.01
26600   10000   0.10    0.02
26600   10000   0.15    0.03
26600   10000   0.20    0.04
40000   5000    0.00    0.00
40000   5000    0.05    0.01
40000   5000    0.10    0.02
40000   5000    0.15    0.03


And I have to organize the f(x,y,z) function in an array of fxyz = [[[],[],[],[]]].

• How can I do it?

• Do you guys recommend another method to create this interpolation function assuming that the data comes in this form?

• Any recommendations?

• Your data do not appear to be on a regular grid. In your example, you are missing 4 values for x=40000. Thus, you should be using an interpolator that does not assume that your data is defined over a grid. – nicoguaro Mar 27 '19 at 12:06

## 1 Answer

As I mentioned in my comment, your data is not defined over a regular grid and that's why you should not be using that function.

One option available in scipy.interpolate is griddata, where you pass your points and values and interpolate in another set of points that you pass. None of them need to be regular. One caveat is that you are going to obtain the interpolation on the convex hull of your data (unless you use the Nearest neighbor method).

Following is an example.

import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Data
data = np.array([
[2.66e+04, 5.00e+03, 5.00e-02, 1.00e-02],
[2.66e+04, 5.00e+03, 1.00e-01, 2.00e-02],
[2.66e+04, 5.00e+03, 1.50e-01, 3.00e-02],
[2.66e+04, 5.00e+03, 2.00e-01, 4.00e-02],
[2.66e+04, 5.00e+03, 2.50e-01, 5.00e-02],
[2.66e+04, 1.00e+04, 5.00e-02, 1.00e-02],
[2.66e+04, 1.00e+04, 1.00e-01, 2.00e-02],
[2.66e+04, 1.00e+04, 1.50e-01, 3.00e-02],
[2.66e+04, 1.00e+04, 2.00e-01, 4.00e-02],
[4.00e+04, 5.00e+03, 0.00e+00, 0.00e+00],
[4.00e+04, 5.00e+03, 5.00e-02, 1.00e-02],
[4.00e+04, 5.00e+03, 1.00e-01, 2.00e-02],
[4.00e+04, 5.00e+03, 1.50e-01, 3.00e-02]])
x, y, z, f = data.T

# Interpolation
grid_x, grid_y, grid_z = np.mgrid[26600:40000:5j, 5000:10000:5j, 0.05:0.20:5j]
grid_f = griddata(data[:, :3], f, (grid_x, grid_y, grid_z))

# Visualization
fig = plt.figure(figsize=(7, 3))
ax0 = fig.add_subplot(121, projection='3d')
ax0.scatter(x, y, z, c=f)
ax1 = fig.add_subplot(122, projection='3d')
ax1.scatter(grid_x.flatten(), grid_y.flatten(), grid_z.flatten(),
c=grid_f.flatten())
plt.show()


Where you obtain the following result. At the left is your original dataset and the interpolated values are on the right.

• Thank you ! Just a question. Whats the little j after the fives in the mgrid definitions ? grid_x, grid_y, grid_z = np.mgrid[26600:40000:5j, 5000:10000:5j, 0.05:0.20:5j] – LM_O Mar 27 '19 at 20:56
• @LM_O, that is an imaginary number. When used with mgrid for the third position it defines the number of subdivisions between the first two values. – nicoguaro Mar 27 '19 at 20:58
• What your opinion on using the Rbf interpolation in scipy ? f_xyz = Rbf(x,y,z,fxyz,function='multiquadric') – LM_O Mar 29 '19 at 23:19
• @LM_O, I can't remember using it. I suggest you ask another question for that matter. – nicoguaro Mar 29 '19 at 23:21