How to get special derivative from an interpolated function

烈酒焚心 提交于 2019-12-23 04:50:13

问题


I have created a h5 file for a simple cube and then read it by python and finally use RegularGridInterpolator function to interpolate. Everything works perfectly for me. But, I want to know how can I change my code so that, I can get derivative from this interpolated function? For your kind information, I have given here my code:

code for creating h5 file
import numpy as np
import h5py

def f(x,y,z):
   return 2 * x**3 + 3 * y**2 - z

x = np.linspace(-1, 1, 2)
y = np.linspace(-1, 1, 2)
z = np.linspace(-1, 1, 2)
mesh_data = f(*np.meshgrid(x, y, z, indexing='ij', sparse=True))

h5file = h5py.File('cube.h5', 'w')
h5file.create_dataset('/x', data=x)
h5file.create_dataset('/y', data=y)
h5file.create_dataset('/z', data=z)
h5file.create_dataset('/mesh_data', data=mesh_data)

h5file.close()

code for reading h5 file and interpolation

import numpy as np   
import h5py
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.interpolate import RegularGridInterpolator
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
f = h5py.File('cube.h5', 'r')  
list(f.keys())
dset = f[u'mesh_data']
dset.shape
dset.value.shape
dset[0:2,0:2,0:2]
x = np.linspace(-1, 1, 2)
y = np.linspace(-1, 1, 2)
z = np.linspace(-1, 1, 2)
my_interpolating_function = RegularGridInterpolator((x, y, z), dset.value, method='nearest')
pts = np.array([0.2, 0.9, 0.6]) 
my_interpolating_function(pts) 

interpolating value is 4.0.


回答1:


I am not sure to understand what you are looking for. Here is a 1D example to illustrate the important points to consider when numerically estimating the derivative of a function:

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d

def f(x, y, z):
    return 2 * x**3 + 3 * y**2 - z

x_fine = np.linspace(-1, 1, 50)  # used for the plots

# Coarse sampling, only two points:
x_coarse = np.linspace(-1, 1, 2)

# Interpolation
interpolator_coarse = interp1d(x_coarse, f(x_coarse, 0, 0), kind='linear')

plt.plot(x_fine, f(x_fine, 0, 0), label='analytical')
plt.plot(x_coarse, f(x_coarse, 0, 0), 'ok', label='coarse sampling')

plt.plot(x_fine, interpolator_coarse(x_fine), '--r', label='interpolation based on the sampling')

plt.xlabel('x'); plt.ylabel('f(x, 0, 0)');
plt.legend();

the graph is:

The value of the 'true' derivative at x=0 is zero (flat slope). However, if the derivative is computed from the sampled data (at x=-1 and x=1), regardless of any kind of interpolation performed, the estimated value would be 2...

The number of sampling points have to be increased:

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d

def f(x, y, z):
    return 2 * x**3 + 3 * y**2 - z

x_fine = np.linspace(-1, 1, 50)  # used for the plots

# Coarse sampling:
x_coarse = np.linspace(-1, 1, 4)

# Interpolation
interpolator_coarse = interp1d(x_coarse, f(x_coarse, 0, 0), kind='linear')
interpolator_cubic = interp1d(x_coarse, f(x_coarse, 0, 0), kind='cubic')

plt.plot(x_fine, f(x_fine, 0, 0), 'k', label='analytical')
plt.plot(x_coarse, f(x_coarse, 0, 0), 'ok', label='coarse sampling')

plt.plot(x_fine, interpolator_coarse(x_fine), '--r', label='linear interpolation')
plt.plot(x_fine, interpolator_cubic(x_fine), '--b', label='cubic interpolation')

plt.xlabel('x'); plt.ylabel('f(x, 0, 0)');
plt.legend();

The slope at x=0 is now closer to zero. The next part of the problem is to estimated the derivative from the sampled data, see for example Numerical_differentiation.



来源:https://stackoverflow.com/questions/54453628/how-to-get-special-derivative-from-an-interpolated-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!