This article describes the n-order curve fitting function implemented by Java. Share it for your reference, as follows:
In the previous article, Java implements a method to solve a single n-order polynomial. After being able to solve a polynomial, it is necessary to use which class to predict future data based on several sample point data. The fitted matrix has been posted in the previous article, so I won’t talk about it here. This article mainly focuses on how to calculate the coefficient matrix based on the sample point and calculate the value of the predicted point.
The principle is very simple, and the formula was also found in the previous article. I will post the code here directly.
The class commonAlgorithm.PolynomiaSoluter written in the previous article is used.
package commonAlgorithm;import commonAlgorithm.PolynomialSluter;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, you must first enter parameters and generate the formula arrayX as the x-axis coordinates of the sampling point, and arrange them in the sampling order * in the order of fitting. When fitting high-order curves with low orders, it may be inaccurate, but too high order will cause slow calculations*/ public boolean generateFormula(double[] arrayX, double[] arrayY, int order) { if (arrayX.length != arrayY.length) return false; this.order = order; int len = arrayX.length; // The x matrix and y matrix in the fitting operation matrixA = new double[order + 1][order + 1]; arrayB = new double[order + 1]; // Generate the y matrix and the part 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 part of 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; } } // Instance PolynomiaSuluter and solve the system of equations to obtain coefficient sequences of each order factors PolynomialSuluter soluter = new PolynomialSuluter(); factors = soluter.getResult(matrixA, arrayB); if (factors == null) return false; else return true; } // The result of calculating the specified coordinates based on the input coordinates and the coefficient sequence factors 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 a few calculation tools for you to refer to:
Online polynomial curves and curve function fitting tools:
http://tools.VeVB.COM/jisuanqi/create_fun/
Draw polynomial/function curve graph tools online:
http://tools.VeVB.COM/jisuanqi/fun_draw
Online unary function (eq) solution calculation tool:
http://tools.VeVB.COM/jisuanqi/equ_jisuanqi
Scientific Calculator Online Use_Advanced Calculator Online Calculator:
http://tools.VeVB.COM/jisuanqi/jsqkeexue
Online Calculator_Standard Calculator:
http://tools.VeVB.COM/jisuanqi/jsq
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.