曲线拟合:一种函数近似,不要求通过数据点,合理反应数据的基本趋势。
拟合函数 φ ( x i ) \varphi(x_i) φ(xi)与原始数据点 _ yi的偏差为:
δ i = φ ( x i ) − y i ( i = 0 , 1 , 2 , ⋯ , n ) \delta_{i}=\varphi\left(x_{i}\right)-y_{i} \quad(i=0,1,2, \cdots, n) δi=φ(xi)−yi(i=0,1,2,⋯,n)
最小二乘法: min ( ∑ i = 1 n δ i 2 ) \min \left(\sum_{i=1}^{n} \delta_{i}^{2}\right) min(∑i=1nδi2)
考虑数据误差时: min ( ∑ i = 1 n ω i δ i 2 ) \min \left(\sum_{i=1}^{n} \omega_i\delta_{i}^{2}\right) min(∑i=1nωiδi2)
当最高幂次 = 1 = 1 m=1时,拟合的是直线,方程组化简为
{ n a 0 + a 1 ∑ i = 1 n x i = ∑ i = 1 n y i a 0 ∑ i = 1 n x i + a 1 ∑ i = 1 n x i 2 = ∑ i = 1 n y i x i \left\{\begin{array}{c} n a_{0}+a_{1} \sum_{i=1}^{n} x_{i}=\sum_{i=1}^{n} y_{i} \\ a_{0} \sum_{i=1}^{n} x_{i}+a_{1} \sum_{i=1}^{n} x_{i}^{2}=\sum_{i=1}^{n} y_{i} x_{i} \end{array}\right. {na0+a1∑i=1nxi=∑i=1nyia0∑i=1nxi+a1∑i=1nxi2=∑i=1nyixi
解线性方程组,即可求得 0 , 1 _0, _1 a0,a1.
X T X = [ 1 1 ⋯ 1 x 1 x 2 ⋯ x n ] [ 1 x 1 1 x 2 ⋮ ⋮ 1 x n ] = [ n ∑ i = 1 n x i ∑ i = 1 n x i ∑ i = 1 n x i 2 ] X^{T} X=\left[\begin{array}{lllr} 1 & 1 & \cdots & 1 \\ x_{1} & x_{2} & \cdots & x_{n} \end{array}\right]\left[\begin{array}{cc} 1 & x_{1} \\ 1 & x_{2} \\ \vdots & \vdots \\ 1 & x_{n} \end{array}\right]\\=\left[\begin{array}{cc} n & \sum_{i=1}^{n} x_{i} \\ \sum_{i=1}^{n} x_{i} & \sum_{i=1}^{n} x_{i}^{2} \end{array}\right] XTX=[1x11x2⋯⋯1xn]⎣⎢⎢⎢⎡11⋮1x1x2⋮xn⎦⎥⎥⎥⎤=[n∑i=1nxi∑i=1nxi∑i=1nxi2]
X T y ⃗ = [ 1 1 ⋯ 1 x 1 x 2 ⋯ x n ] [ y 1 y 2 ⋮ y n ] = [ ∑ i = 1 n y i ∑ i = 1 n x i y i ] X^{T} \vec{y}=\left[\begin{array}{cccc} 1 & 1 & \cdots & 1 \\ x_{1} & x_{2} & \cdots & x_{n} \end{array}\right]\left[\begin{array}{c} y_{1} \\ y_{2} \\ \vdots \\ y_{n} \end{array}\right]\\=\left[\begin{array}{l} \sum_{i=1}^{n} y_{i} \\ \sum_{i=1}^{n} x_{i} y_{i} \end{array}\right] XTy=[1x11x2⋯⋯1xn]⎣⎢⎢⎢⎡y1y2⋮yn⎦⎥⎥⎥⎤=[∑i=1nyi∑i=1nxiyi]
法方程化为
[ n ∑ i = 1 n x i ∑ i = 1 n x i ∑ i = 1 n x i 2 ] [ [ β 0 β 1 ] = [ ∑ i = 1 n y i ∑ i = 1 n x i y i ] \left[\begin{array}{cc} n & \sum_{i=1}^{n} x_{i} \\ \sum_{i=1}^{n} x_{i} & \sum_{i=1}^{n} x_{i}^{2} \end{array}\right]\left[\begin{array}{c} {\left[\begin{array}{c} \beta_{0} \\ \beta_{1} \end{array}\right]=\left[\begin{array}{c} \sum_{i=1}^{n} y_{i} \\ \sum_{i=1}^{n} x_{i} y_{i} \end{array}\right]} \end{array}\right. [n∑i=1nxi∑i=1nxi∑i=1nxi2][[β0β1]=[∑i=1nyi∑i=1nxiyi]
即:
{ n β 0 + β 1 ∑ i = 1 n x i = ∑ i = 1 n y i β 0 ∑ i = 1 n x i + β 1 ∑ i = 1 n x i 2 = ∑ i = 1 n y i x i \left\{\begin{array}{c} n \beta_{0}+\beta_{1} \sum_{i=1}^{n} x_{i}=\sum_{i=1}^{n} y_{i} \\ \beta_{0} \sum_{i=1}^{n} x_{i}+\beta_{1} \sum_{i=1}^{n} x_{i}^{2}=\sum_{i=1}^{n} y_{i} x_{i} \end{array}\right. {nβ0+β1∑i=1nxi=∑i=1nyiβ0∑i=1nxi+β1∑i=1nxi2=∑i=1nyixi
np.polyfit
实例代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
t = np.ogrid[0:5.5:0.5]
s = t+ 0.2*t**2
s1 = np.array([0.5,-0.18,-0.01,0.13,0.1,0.31,-0.22,-0.31,0.2,0.4,-0.14]) # 假设误差
ss = s+s1
P = np.polyfit(t,ss,2) # 二次曲线拟合
y = np.polyval(P,t) # 利用参数拟合函数值
plt.plot(t,ss,"r*",t,y,"g",lw=4,ms=10)