对于一般多项式:
K为多项式最高项次,a为不确定的常数项,共k+1个;
有离散数据集对应,其方差:
β为,方差函数S对β自变量第j个参数的梯度(偏导数):
当以上梯度为零时,S函数值最小,即:
中的每个每个偏导数构成一个等式:
...
则:
...
变为矩阵形式:
这样就变成线性方程求解形式,可用高斯消元等方法求得,注意在计算过程中要判断对角线上的值是否为零,如果等于零可以通过换行的方法解决;
1 /// <summary> 2 /// Function y = a0+a1*x+a2*x^2+ ... + an*x^n 3 /// </summary> 4 /// <param name="dataX">x values</param> 5 /// <param name="Y">y values</param> 6 /// <param name="N">Degree of a Polynomial</param> 7 /// <returns>[a0,a1,...,an]</returns> 8 public static double[] PolyRegress(double[] dataX, double[] Y,int N) 9 { 10 const double tiny = 0.00001; 11 int M = dataX.Length; // M = Number of data points 12 int D = N + 1; 13 double x = 0, y = 0; 14 double[,] V = new double[D, D]; 15 double[] A = new double[D]; 16 for (int i = 0; i < M; i++) 17 { 18 x = dataX[i]; 19 y = Y[i]; 20 for(int j=0;j<D;j++) 21 { 22 for(int k=0;k<D;k++) 23 { 24 V[j, k] += Math.Pow(x, j + k); 25 } 26 A[j] += y * Math.Pow(x, j); 27 } 28 29 } 30 for (int i = 0; i < D; i++) 31 { 32 double m = V[i, i]; 33 if (Math.Abs(m) < tiny) 34 { 35 for (int i2 = i + 1; i2 < D; i2++) 36 { 37 if (Math.Abs(V[i2, i]) > tiny) 38 { 39 double tmp = 0; 40 for (int c = 0; c < D; c++) 41 { 42 tmp = V[i, c]; 43 V[i, c] = V[i2, c]; 44 V[i2, c] = tmp; 45 } 46 tmp = A[i]; 47 A[i] = A[i2]; 48 A[i2] = tmp; 49 break; 50 } 51 } 52 m = V[i, i]; 53 } 54 55 if (Math.Abs(m) > tiny) 56 { 57 for (int j = i; j < D; j++) 58 { 59 V[i, j] /= m; 60 } 61 A[i] /= m; 62 for (int k = 0; k < D; k++) 63 { 64 if (k != i) 65 { 66 m = V[k, i]; 67 for (int l = i; l < D; l++) 68 { 69 V[k, l] -= m * V[i, l]; 70 } 71 A[k] -= m * A[i]; 72 } 73 } 74 } 75 } 76 return A; 77 }