问题描述
实际上:
f
(
a
)
=
∫
(
g
−
∑
0
n
a
i
x
i
)
2
=
∫
g
2
+
∫
(
∑
0
n
a
i
x
i
)
2
−
2
∫
g
∑
0
n
a
i
x
i
f(a)=\int(g-\sum_0^na_ix^i)^2=\int g^2+\int (\sum_0^na_ix^i)^2-2\int g\sum_0^na_ix^i
f(a)=∫(g−∑0naixi)2=∫g2+∫(∑0naixi)2−2∫g∑0naixi
求偏导数:
∂
f
(
a
)
∂
a
k
=
2
∫
(
∑
0
n
a
i
x
i
)
x
k
−
2
∫
g
∗
x
k
=
0
\frac{\partial f (a)}{\partial a_k} =2\int (\sum_0^na_ix^i)x^k-2\int g*x^k=0
∂ak∂f(a)=2∫(∑0naixi)xk−2∫g∗xk=0
等价于:
∑
i
=
0
n
a
i
i
+
k
+
1
=
∫
g
∗
x
k
,
k
=
0
,
.
.
.
,
n
\sum_{i=0}^n \frac{a_i}{i+k+1}=\int g*x^k,k=0,...,n
∑i=0ni+k+1ai=∫g∗xk,k=0,...,n。
写成矩阵格式:
[
1
1
/
2
1
/
3
⋯
1
/
(
n
+
1
)
1
/
2
1
/
3
1
/
4
⋯
1
/
(
n
+
2
)
1
/
3
1
/
4
1
/
5
1
/
(
n
+
3
)
⋮
⋮
⋮
⋱
1
/
(
n
+
1
)
1
/
(
n
+
2
)
1
/
(
n
+
3
)
⋯
1
/
(
2
n
+
1
)
]
[
a
0
a
1
a
2
⋮
a
n
]
=
[
g
0
g
1
g
2
⋮
g
n
]
\left[ \begin{array}{ccccc} 1 & 1 / 2 & 1 / 3 & \cdots & 1 /(n+1)\\ 1 / 2 & 1 / 3 & 1 / 4 & \cdots & 1 / (n + 2)\\ 1 / 3 & 1 / 4 & 1 / 5 & & 1 / (n + 3)\\ \vdots & \vdots & \vdots & \ddots & \\ 1 / (n+1) & 1 / (n + 2) & 1 / (n + 3) & \cdots & 1 / (2 n +1) \end{array} \right] \left[ \begin{array}{c} a_0\\ a_1\\ a_2\\ \vdots\\ a_n \end{array} \right] = \left[ \begin{array}{c} g_0\\ g_1\\ g_2\\ \vdots\\ g_n \end{array} \right]
⎣⎢⎢⎢⎢⎢⎡11/21/3⋮1/(n+1)1/21/31/4⋮1/(n+2)1/31/41/5⋮1/(n+3)⋯⋯⋱⋯1/(n+1)1/(n+2)1/(n+3)1/(2n+1)⎦⎥⎥⎥⎥⎥⎤⎣⎢⎢⎢⎢⎢⎡a0a1a2⋮an⎦⎥⎥⎥⎥⎥⎤=⎣⎢⎢⎢⎢⎢⎡g0g1g2⋮gn⎦⎥⎥⎥⎥⎥⎤
其中: g k = ∫ g ∗ x k , k = 0 , . . . , n g_k=\int g*x^k,k=0,...,n gk=∫g∗xk,k=0,...,n
也就是: A B = G AB=G AB=G。
求解
Mathematica求解如下:
fit[g_, n_] :=
Module[{G = Table[Integrate[g[x]*x^k, {x, 0, 1}], {k, 0, n}],
A = Table[1/(i + j + 1), {i, 0, n}, {j, 0, n}]},
LinearSolve[A, G].Table[x^k, {k, 0, n}]]
测试:
g[x_] := Sin[2.*Pi*x];
n = 3;
l = fit[g, n];
Plot[{l, g[x]}, {x, 0, 1}, PlotLabel -> StringJoin["n=", ToString[n]],
Frame -> True]
输出为