目录
专栏:数学建模学习笔记
第一部分:插值的基本原理及应用
1. 插值的基本原理
插值是一种在已知数据点之间估算函数值的方法。它在数据分析、信号处理和数值分析中具有广泛应用。插值的目标是通过构造一个插值函数,使该函数在给定的数据点处具有精确的函数值。
1.1 插值多项式
1.2 拉格朗日插值
import numpy as np
import matplotlib.pyplot as plt
# 拉格朗日基函数
def lagrange_basis(x, x_values, j):
basis = 1
for i in range(len(x_values)):
if i != j:
basis *= (x - x_values[i]) / (x_values[j] - x_values[i])
return basis
# 拉格朗日插值多项式
def lagrange_interpolation(x, x_values, y_values):
interpolation = 0
for j in range(len(y_values)):
interpolation += y_values[j] * lagrange_basis(x, x_values, j)
return interpolation
# 数据点
x_values = np.array([0, 1, 2, 3, 4, 5])
y_values = np.array([1, 3, 2, 5, 7, 8])
# 插值点
x_interp = np.linspace(0, 5, 100)
y_interp = [lagrange_interpolation(x, x_values, y_values) for x in x_interp]
# 绘图
plt.plot(x_values, y_values, 'o', label='Data points')
plt.plot(x_interp, y_interp, '-', label='Lagrange Interpolation')
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.show()
1.3 牛顿插值
import numpy as np
import matplotlib.pyplot as plt
# 计算差分商
def divided_diff(x_values, y_values):
n = len(x_values)
coef = np.zeros([n, n])
coef[:,0] = y_values
for j in range(1,n):
for i in range(n-j):
coef[i][j] = (coef[i+1][j-1] - coef[i][j-1]) / (x_values[i+j] - x_values[i])
return coef[0,:]
# 牛顿插值多项式
def newton_interpolation(x, x_values, coef):
n = len(x_values) - 1
p = coef[n]
for k in range(1,n+1):
p = coef[n-k] + (x -x_values[n-k])*p
return p
# 数据点
x_values = np.array([0, 1, 2, 3, 4, 5])
y_values = np.array([1, 3, 2, 5, 7, 8])
# 计算差分商系数
coef = divided_diff(x_values, y_values)
# 插值点
x_interp = np.linspace(0, 5, 100)
y_interp = [newton_interpolation(x, x_values, coef) for x in x_interp]
# 绘图
plt.plot(x_values, y_values, 'o', label='Data points')
plt.plot(x_interp, y_interp, '-', label='Newton Interpolation')
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.show()
1.4 样条插值
样条插值是一种分段插值方法。常见的样条插值包括线性样条和三次样条。三次样条插值具有良好的光滑性和逼近性能,是一种常用的插值方法。
三次样条插值代码示例:
import numpy as np
from scipy.interpolate import CubicSpline
import matplotlib.pyplot as plt
# 数据点
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([1, 3, 2, 5, 7, 8])
# 创建三次样条插值对象
cs = CubicSpline(x, y)
# 插值点
x_interp = np.linspace(0, 5, 100)
y_interp = cs(x_interp)
# 绘图
plt.plot(x, y, 'o', label='Data points')
plt.plot(x_interp, y_interp, '-', label='Cubic Spline Interpolation')
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.show()
2. 插值的Python实现
Python 提供了丰富的库来实现插值方法,主要包括 NumPy 和 SciPy 库。
2.1 使用 NumPy 进行插值
NumPy 提供了一些基本的插值函数,例如 numpy.interp
可以进行一维线性插值。
import numpy as np
import matplotlib.pyplot as plt
# 原始数据点
x = np.linspace(0, 10, 10)
y = np.sin(x)
# 插值点
x_interp = np.linspace(0, 10, 100)
y_interp = np.interp(x_interp, x, y)
# 绘图
plt.plot(x, y, 'o', label='Original data')
plt.plot(x_interp, y_interp, '-', label='Interpolated data')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.show()
2.2 使用 SciPy 进行插值
SciPy 提供了更加全面的插值函数,例如 scipy.interpolate.interp1d
和 scipy.interpolate.CubicSpline
。
2.2.1 一维插值
from scipy.interpolate import interp1d
import numpy as np
import matplotlib.pyplot as plt
# 原始数据点
x = np.linspace(0, 10, 10)
y = np.sin(x)
# 创建插值对象
linear_interp = interp1d(x, y, kind='linear')
cubic_interp = interp1d(x, y, kind='cubic')
# 插值点
x_interp = np.linspace(0, 10, 100)
y_linear = linear_interp(x_interp)
y_cubic = cubic_interp(x_interp)
# 绘图
plt.plot(x, y, 'o', label='Original data')
plt.plot(x_interp, y_linear, '-', label='Linear interpolation')
plt.plot(x_interp, y_cubic, '--', label='Cubic interpolation')
plt.legend()
plt.show()
2.2.2 二维插值
from scipy.interpolate import RectBivariateSpline
import numpy as np
import matplotlib.pyplot as plt
# 原始数据点
x = np.linspace(0, 10, 10)
y = np.linspace(0, 10, 10)
z = np.sin(x[:, None] + y[None, :])
# 创建插值对象
linear_interp = RectBivariateSpline(x, y, z, kx=1, ky=1)
cubic_interp = RectBivariateSpline(x, y, z, kx=3, ky=3)
# 插值点
x_interp = np.linspace(0, 10, 100)
y_interp = np.linspace(0, 10, 10