【Python数值分析】革命:引领【数学建模】新时代的插值与拟合前沿技术_python插值拟合
目录
编辑
第一部分:插值的基本原理及应用
1. 插值的基本原理
1.1 插值多项式
1.2 拉格朗日插值
1.3 牛顿插值
1.4 样条插值
2. 插值的Python实现
2.1 使用 NumPy 进行插值
2.2 使用 SciPy 进行插值
2.2.1 一维插值
编辑
2.2.2 二维插值
3. 插值的应用场景
3.1 数据平滑和填补
3.2 图像处理
编辑
3.3 数值模拟
4. 实例分析
实例1:空气质量数据的校准
编辑
实例2:波浪能最大输出功率设计
第二部分:拟合的基本原理及应用
1. 拟合的基本原理
1.1 线性拟合
1.2 多项式拟合
1.3 指数拟合
编辑
1.4 对数拟合
编辑
1.5 幂函数拟合
2. 拟合的Python实现
2.1 使用 SciPy 进行拟合
2.1.1 线性拟合
2.1.2 多项式拟合
2.1.3 指数拟合
2.1.4 对数拟合
2.1.5 幂函数拟合
3. 拟合的应用场景
3.1 数据预测
3.2 数据建模
3.3 物理实验数据分析
3.4 工程设计
4. 实例分析
实例1:股票价格预测
实例2:温度变化分析
总结
专栏:数学建模学习笔记
第一部分:插值的基本原理及应用
1. 插值的基本原理
插值是一种在已知数据点之间估算函数值的方法。它在数据分析、信号处理和数值分析中具有广泛应用。插值的目标是通过构造一个插值函数,使该函数在给定的数据点处具有精确的函数值。
1.1 插值多项式
1.2 拉格朗日插值
import numpy as npimport 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 npimport 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 npfrom scipy.interpolate import CubicSplineimport 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 npimport 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 interp1dimport numpy as npimport 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 RectBivariateSplineimport numpy as npimport 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