Because I am in the project to use the least squares fitting, all of my time to the C + + implementation of the program to the Java implementation, now posted out for everyone to refer to the use.
/**
* <p> function function: least squares curve fitting </p>
* @param x Real one-dimensional array of length n. The X-coordinate of the given n number of locations
* @param y Real one-dimensional array of length n. Holds the Y-coordinate of a given n number of locations
* @param n variables. Number of given data points
* @param a real one-dimensional array with a length of M. Returns the M coefficients of a m-1-fitting polynomial
* @param m fits the number of items in the polynomial, that is, the maximum number of times to fit the polynomial is m-1.
* Requires M<=n and m<=20. If M>n or m>20, this function is processed automatically by m=min{n,20}.
* <p>date:2007-12-25 16:21 pm</p>
* @author Qingbao-gao
* @return
*/
public static double[] Polyfit (double x[], double y[], int n, double a[], int m)
{
int I, j, K;
Double Z, p, c, g, q = 0, D1, D2;
Double []s=new double[20];
Double []t=new double[20];
Double[] B=new double[20];
Double[]dt=new Double[3];
for (i = 0; I <= m-1; i++)
{
A[i] = 0.0;
}
if (M > N)
{
m = n;
}
if (M > 20)
{
m = 20;
}
z = 0.0;
for (i = 0; I <= n-1; i++)
{
z = z+x[i]/(1.0 *n);
}
B[0] = 1.0;
D1 = 1.0 * N;
p = 0.0;
c = 0.0;
for (i = 0; I <= n-1; i++)
{
p = p+ (x[i]-z);
c = C+y[i];
}
c = c/d1;
p = p/d1;
A[0] = c * B[0];
if (M > 1)
{
T[1] = 1.0;
T[0] =-P;
D2 = 0.0;
c = 0.0;
g = 0.0;
for (i = 0; I <= n-1; i++)
{
Q = x[i]-z-p;
D2 = d2+q * Q;
c = C+y[i] *q;
g = g+ (x[i]-z) *q * q;
}
c = c/d2;
p = g/d2;
Q = d2/d1;
D1 = D2;
A[1] = c * T[1];
A[0] = c * T[0]+a[0];
}
for (j = 2; J <= M-1; j + +)
{
S[J] = t[j-1];
S[J-1] =-P * T[j-1]+t[j-2];
if (J >= 3)
for (k = j-2; k >= 1; k--)
{
S[K] =-P * t[k]+t[k-1]-q * b[k];
}
S[0] =-P * t[0]-q * b[0];
D2 = 0.0;
c = 0.0;
g = 0.0;
for (i = 0; I <= n-1; i++)
{
Q = s[j];
for (k = j-1; k >= 0; k--)
{
Q = q * (x[i]-z) +s[k];
}
D2 = d2+q * Q;
c = C+y[i] *q;
g = g+ (x[i]-z) *q * q;
}
c = c/d2;
p = g/d2;
Q = d2/d1;
D1 = D2;
A[J] = c * S[J];
T[J] = S[j];
for (k = j-1; k >= 0; k--)
{
A[K] = c * S[k]+a[k];
B[K] = t[k];
T[K] = s[k];
}
}
Dt[0] = 0.0;
DT[1] = 0.0;
DT[2] = 0.0;
for (i = 0; I <= n-1; i++)
{
Q = a[m-1];
for (k = m-2; k >= 0; k--)
{
Q = a[k]+q * (x[i]-z);
}
p = q-y[i];
if (Math.Abs (P) > Dt[2])
{
DT[2] = Math.Abs (p);
}
DT[0] = dt[0]+p * p;
DT[1] = Dt[1]+math.abs (p);
}
return A;
}
/**
* <p> average penalty for x-axis data section </p>
* @param x stores an array of x-axis nodes
* <p>date:2007-12-25 20:21 pm</p>
* @author Qingbao-gao
* @return Average
*/
public static Double Ave (double []x)
{
Double ave=0;
Double sum=0;
if (x!=null)
{
for (int i=0;i<x.length;i++)
{
Sum+=x[i];
}
System.out.println ("sum-->" +sum);
Ave=sum/x.length;
System.out.println ("Ave" +ave+ "X.length" +x.length);
}
Return Ave;
}
/**
* <p> y value obtained by x value </p>
* @param x Current x-axis input value, which is the predicted month
* @param xx current x-axis input value of the first X data points
* @param a storage array of polynomial coefficients
* @param m the highest number of arrays of stored polynomial
* <p>date:2007-12-25 PM 20:07</p>
* <P>Author:qingbao-gao</P>
* @return The y-axis value corresponding to the X-axis node value
*/
public static double GetY (double x,double[]xx,double[]a,int m)
{
Double y=0;
Double Ave=ave (XX);
Double l=0;
for (int i=0;i<m;i++)
{
L=A[0];
if (i>0)
{
Y+=a[i]*math.pow ((x-ave), i);
System.out.println (i+ "--|-->" +y+ "--a[i]--" +a[i]);
}
System.out.println ("a[0]|" +a[0]);
}
System.out.println ("l--|" + (l));
return (Y+L);
}
--------------------------------------------Test Code
public static void Main (String []args) throws Dbexception
{
Double []x={ 200401,200402,200403,200404,200405,200406,200407,200408,200409,2004010,2004011,2004012,200501,200502,200503,200504 };
double []y={51,51,53,53,54,55,57,60,63,64,66,66,69,71,72,75};
Double[]a=new DOUBLE[20];
Double[]aa= polyfit (x, Y, 16,a, 3);
Double yy=0;
System.out.println ("Fit-to" +gety (200505,x,aa,3));
}
The test result is: fitting-->72.38898870320554
The effect is also possible.
Http://www.blogjava.net/1504/archive/2009/04/15/265869.html
Least squares method to fit Java Implementation source program (RPM)