Java-implemented n-order Curve Fitting Function example, java example
This example describes the n-order Curve Fitting Function implemented by Java. We will share this with you for your reference. The details are as follows:
In the previous article, Java implements the method for solving the n-degree polynomial of a dollar. After the polynomial can be solved, the class also needs to be used to predict future data based on several sampling points, the fitting matrix has been posted in the previous article. I will not mention it here. This article mainly describes how to calculate the coefficient matrix based on the sampling points and calculate the value of the prediction point.
The principle is very simple. The formula is also available in the previous article. The code is directly pasted here.
The class written in the previous article is used.commonAlgorithm.PolynomiaSoluter
Package commonAlgorithm; import commonAlgorithm. polynomialSoluter; import java. lang. math; public class LeastSquare {private double [] [] matrixA; private double [] arrayB; private double [] factors; private int order; public LeastSquare () {}/** after instantiation, before calculation, enter a parameter and generate the formula arrayX, which is the X axis coordinate of the sample point. * arrayY is the Y axis coordinate of the sample point, order * is the order for fitting according to the sampling sequence and the order * corresponding to x. It may be inaccurate to fit a higher-order curve with a lower order, but an excessively high order may lead to slow calculation */public boolean generateFormula (double [] arrayX, double [] arrayY, int order) {if (arrayX. length! = ArrayY. length) return false; this. order = order; int len = arrayX. length; // x matrix in the fitting operation and y matrix matrixA = new double [order + 1] [order + 1]; arrayB = new double [order + 1]; // generate the y matrix and the portion of the power <= order in the x matrix for (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 the power> order part of 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 the equation group to obtain the coefficient sequence factors PolynomialSoluter soluter = new PolynomialSoluter (); factors = soluter. getResult (matrixA, arrayB); if (factors = null) return false; else return true;} // based on the input coordinates, public double calculate (double x) {double result = factors [0]; for (int I = 1; I <= order; I ++) result + = factors [I] * Math. pow (x, I); return result ;}}
PS: Here are some recommended computing tools for your reference:
Online polynomial curve and curve function fitting tools:
Http://tools.jb51.net/jisuanqi/create_fun/
Online drawing of Polynomial/Function Curve graphics tools:
Http://tools.jb51.net/jisuanqi/fun_draw
Calculation tool for online mona1 functions (equations:
Http://tools.jb51.net/jisuanqi/equ_jisuanqi
Scientific calculator online use _ advanced calculator online computing:
Http://tools.jb51.net/jisuanqi/jsqkexue
Online calculator _ standard calculator:
Http://tools.jb51.net/jisuanqi/jsq