After an article, can solve the polynomial, also need to use that class, based on a number of sampling point data to predict future data, the fitting matrix in the previous article has been posted, here is not to say, this article is mainly how to calculate the coefficient matrix based on sampling points, and calculate the value of the predicted point.
The principle is very simple, the formula also has in the previous article, here directly pastes the code.
It used the class Commonalgorithm.polynomiasoluter written in the previous article
Package commonalgorithm;
Import Commonalgorithm.polynomialsoluter;
Import Java.lang.Math;
public class Leastsquare {private double[][] Matrixa;
Private double[] Arrayb;
private double[] factors;
private int order; After the public Leastsquare () {} * * is instantiated, the calculation is preceded by an input parameter and a formula Arrayx as the x-axis coordinates of the sampling point, in the order of sampling the y-coordinate of the arrayy as the sampling point, in the order of sampling and X One by one order of order * for the number of orders to be fitted.
It may be inaccurate to fit higher-order curves with lower order, but too high order can result in slow computation/public boolean generateformula (double[] Arrayx, double[] arrayy, int orders) {
if (arrayx.length!= arrayy.length) return false;
This.order = order;
int len = arrayx.length;
The x and Y matrices in the fitting operation Matrixa = new Double[order + 1][order + 1];
Arrayb = new Double[order + 1];
Generates a Y-matrix and a partial for-power <=order in the X-matrix (int i = 0; i < order + 1; i++) {double sumx = 0;
for (int j = 0; J < Len; J +) {Double tmp = MATH.POW (arrayx[j], i); SUMX = tmp
Arrayb[i] + = tmp * ARRAYY[J];
for (int j = 0; J <= i; j +) matrixa[j][i-j] = sumx;
}//Generate a portion of the power >order in the x matrix for (int i = order + 1; I <= order * 2; i++) {double sumx = 0;
for (int j = 0; J < Len; j +) Sumx + + = Math.pow (Arrayx[j], i);
for (int j = I-order, J < order + 1; j + +) {Matrixa[i-j][j] = sumx; }///instantiate Polynomiasoluter and solve equations, get the coefficients sequence of each order factors polynomialsoluter soluter = new Polynomialsolute
R ();
factors = Soluter.getresult (Matrixa, Arrayb);
if (factors = null) return false;
else return true;
The result of the specified coordinates, based on the input coordinates and the factors of the coefficient sequence, public double calculate (double x) {Double results = factors[0];
for (int i = 1; I <= order; i++) result + = factors[i] * MATH.POW (x, i); return result;
}
}