繁体   English   中英

如何从插值函数获得特殊导数

[英]How to get special derivative from an interpolated function

我为一个简单的多维数据集创建了一个h5文件,然后通过python读取了该文件,最后使用RegularGridInterpolator函数进行插值。 一切对我来说都很完美。 但是,我想知道如何更改代码,以便可以从此插值函数中派生代码? 为了给您提供信息,我在这里给出了我的代码:

用于创建h5文件的代码
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) 

读取h5文件和插值的代码

 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) 

插值是4.0。

我不确定您要寻找什么。 这是一维示例,用于说明在对函数的导数进行数值估计时要考虑的要点:

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();

该图是:

图形

x = 0时“真”导数的值为零(平坦斜率)。 但是,如果根据采样数据(在x = -1和x = 1时)计算导数,则无论执行哪种插值,估计值都将为2 ...

必须增加采样点的数量:

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();

4分

x = 0处的斜率现在更接近于零。 问题的下一部分是从采样数据中估计导数,例如,参见numeric_differentiation

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM