求其最小二乘逼近,取权系数为1,基函数:
φ
1
(
x
)
=
1
,
φ
2
(
x
)
=
x
,
φ
3
(
x
)
=
x
2
,
φ
4
(
x
)
=
x
3
\varphi_{1}(x)=1, \varphi_{2}(x)=x, \varphi_{3}(x)=x^{2}, \varphi_{4}(x)=x^{3}
φ1(x)=1,φ2(x)=x,φ3(x)=x2,φ4(x)=x3
Φ
1
=
(
1
,
1
,
1
,
…
,
1
,
1
)
T
Φ
2
=
(
−
1
,
−
0.75
,
−
0.5
,
−
0.25
,
0
,
0.25
,
0.5
,
0.75
,
1
,
1.25
)
T
Φ
3
=
(
(
−
1
)
2
,
(
−
0.75
)
2
,
(
−
0.5
)
2
,
(
−
0.25
)
2
,
0
2
,
0.2
5
2
,
0.
5
2
,
0.7
5
2
,
1
2
,
1.2
5
2
)
T
Φ
4
=
(
−
1
3
,
−
0.7
5
3
,
−
0.
5
3
,
−
0.2
5
3
,
0
3
,
0.2
5
3
,
0.
5
3
,
0.7
5
3
,
1
3
,
1.2
5
3
)
T
Y
=
(
0.2209
,
0.3295
,
0.8826
,
1.4392
,
2.0003
,
2.5645
,
3.1334
,
3.7061
,
4.2836
,
3.1334
)
T
\begin{array}{c} \Phi_{1}=(1,1,1, \ldots, 1,1)^{T} \\ \Phi_{2}=(-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1,1.25)^{T} \\ \Phi_{3}=\left((-1)^{2},(-0.75)^{2},(-0.5)^{2},(-0.25)^{2}, 0^{2}, 0.25^{2}, 0.5^{2}, 0.75^{2}, 1^{2}, 1.25^{2}\right)^{T} \\ \Phi_{4}=\left(-1^{3},-0.75^{3},-0.5^{3},-0.25^{3}, 0^{3}, 0.25^{3}, 0.5^{3}, 0.75^{3}, 1^{3}, 1.25^{3}\right)^{T} \\ Y=(0.2209,0.3295,0.8826,1.4392,2.0003,2.5645,3.1334,3.7061,4.2836,3.1334)^{T} \end{array}
Φ1=(1,1,1,…,1,1)TΦ2=(−1,−0.75,−0.5,−0.25,0,0.25,0.5,0.75,1,1.25)TΦ3=((−1)2,(−0.75)2,(−0.5)2,(−0.25)2,02,0.252,0.52,0.752,12,1.252)TΦ4=(−13,−0.753,−0.53,−0.253,03,0.253,0.53,0.753,13,1.253)TY=(0.2209,0.3295,0.8826,1.4392,2.0003,2.5645,3.1334,3.7061,4.2836,3.1334)T
一次多项式拟合数据:
拟
合
函
数
:
f
(
x
)
=
∂
1
+
∂
2
x
拟合函数: f(x)=\partial_{1}+\partial_{2} x
拟合函数:f(x)=∂1+∂2x
二次多项式拟合数据:
拟
合
函
数
:
f
(
x
)
=
∂
1
+
∂
2
x
+
∂
3
x
2
拟合函数: f(x)=\partial_{1}+\partial_{2} x+\partial_{3} x^{2}
拟合函数:f(x)=∂1+∂2x+∂3x2
三次多项式拟合数据:
拟
合
函
数
:
f
(
x
)
=
∂
1
+
∂
2
x
+
∂
3
x
2
+
∂
4
x
3
拟合函数: f(x)=\partial_{1}+\partial_{2} x+\partial_{3} x^{2}+\partial_{4} x^{3}
拟合函数:f(x)=∂1+∂2x+∂3x2+∂4x3
误
差
计
算
:
∑
n
=
1
10
[
Y
n
−
f
(
x
n
)
]
2
误差计算: \sum_{n=1}^{10}\left[Y_{n}-f\left(x_{n}\right)\right]^{2}
误差计算:n=1∑10[Yn−f(xn)]2
程序求解:
import numpy as np
A1 = np.array([[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]])
A2 = np.array([[-1], [-0.75], [-0.5], [-0.25], [0], [0.25], [0.5], [0.75], [1], [1.25]])
A3 = np.array([[(-1)**2], [(-0.75)**2], [(-0.5)**2], [(-0.25)**2], [0**2], [0.25**2], [0.5**2], [0.75**2], [1**2], [1.25**2]])
A4 = np.array([[-1**3], [-0.75**3], [-0.5**3], [-0.25**3], [0**3], [0.25**3], [0.5**3], [0.75**3], [1**3], [1.25**3]])
B = np.array([[0.2209], [0.3295], [0.8826], [1.4392], [2.0003], [2.5645], [3.1334], [3.7061], [4.2836], [3.1334]])
# 一次
a1 = np.array([[np.dot(A1.T, A1), np.dot(A1.T, A2)],
[np.dot(A2.T, A1), np.dot(A2.T, A2)]]).reshape(2,2)
b1 = np.array([[np.dot(B.T, A1)], [np.dot(B.T, A2)]]).reshape(2,1)
c1 = np.linalg.solve(a1, b1)
print(c1) #拟合系数
C1 = c1[0] + c1[1] * A1
D1 = (B-C1)**2
print(np.sum(D1)) #误差
# 二次
a2 = np.array([[np.dot(A1.T, A1), np.dot(A1.T, A2), np.dot(A1.T, A3)],
[np.dot(A2.T, A1), np.dot(A2.T, A2), np.dot(A2.T, A3)],
[np.dot(A3.T, A1), np.dot(A3.T, A2), np.dot(A3.T, A3)]]).reshape(3,3)
b2 = np.array([[np.dot(B.T, A1)], [np.dot(B.T, A2)], [np.dot(B.T, A3)]]).reshape(3,1)
c2 = np.linalg.solve(a2, b2)
print(c2) #拟合系数
C2 = c2[0] + c2[1] * A1 + c2[2] * A1**2
D2 = (B-C2)**2
print(np.sum(D2)) #误差
# 三次
a3 = np.array([[np.dot(A1.T, A1), np.dot(A1.T, A2), np.dot(A1.T, A3), np.dot(A1.T, A4)],
[np.dot(A2.T, A1), np.dot(A2.T, A2), np.dot(A2.T, A3), np.dot(A2.T, A4)],
[np.dot(A3.T, A1), np.dot(A3.T, A2), np.dot(A3.T, A3), np.dot(A3.T, A4)],
[np.dot(A4.T, A1), np.dot(A4.T, A2), np.dot(A4.T, A3), np.dot(A4.T, A4)]]).reshape(4,4)
b3 = np.array([[np.dot(B.T, A1)], [np.dot(B.T, A2)], [np.dot(B.T, A3)], [np.dot(B.T, A4)]]).reshape(4,1)
c3 = np.linalg.solve(a3, b3)
print(c3) #拟合系数
C3 = c3[0] + c3[1] * A1 + c3[2] * A1**2 + c3[3] * A1**3
D3 = (B-C3)**2
print(np.sum(D3)) #误差
从多项式拟合数据的误差平方和来看,二次多项式的拟合效果相对较好,其次是一次多项式拟合,三次多项式拟合效果相对较差。
如有错误或者不足之处,欢迎大家留言指正!