An example of n level curve fitting function implemented by Java

  • 2020-12-26 05:46:43
  • OfStack

The example of Java illustrates the n curve fitting function. To share for your reference, the details are as follows:

Front 1 article Java implementation solution of 1 yuan n polynomial method, polynomial can solution later, still need to use the class, according to the number of sampling point data to forecast the future data, the fitting of the matrix in 1 article was posted on here is not to say, this is how the coefficient matrix is calculated according to sample point, and calculate the predicted value.

The principle is very simple, the formula was also found in the previous article, post the code directly here.

It uses the classes written in the previous article 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, input parameters and generate formulas before calculation  arrayX Is of the sampling point x Axis coordinates, arranged in sampling order 
   * arrayY Is of the sampling point y Axis coordinates, in sampling order and x11 Corresponding arrangement  order
   *  Is the order of fitting. It may be inaccurate to use the lower order to fit the higher order curve, but too high order will 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;
    //  In the fitting operation x Matrix and y matrix 
    matrixA = new double[order + 1][order + 1];
    arrayB = new double[order + 1];
    //  generate y Matrix and x In the matrix power <=order Part of the 
    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 x In the matrix power >order Part of the 
    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;
      }
    }
    //  instantiation PolynomiaSoluter And solve the system of equations, get the coefficient sequence of each order factors
    PolynomialSoluter soluter = new PolynomialSoluter();
    factors = soluter.getResult(matrixA, arrayB);
    if (factors == null)
      return false;
    else
      return true;
  }
  //  According to the input coordinates, and the coefficient sequence factors Evaluates the result of the specified 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 a few more computing tools for you to refer to in the next step:

On-line fitting tools for polynomial curves and curve functions:
http://tools.ofstack.com/jisuanqi/create_fun/

Graph tool for drawing polynomial/function curves online:
http://tools.ofstack.com/jisuanqi/fun_draw

Online 1 element function (equation) solving calculation tool:
http://tools.ofstack.com/jisuanqi/equ_jisuanqi

Scientific Calculators online Using _ Advanced calculators online calculation:
http://tools.ofstack.com/jisuanqi/jsqkexue

Online calculators _ Standard calculators:
http://tools.ofstack.com/jisuanqi/jsq

For more information about java algorithm, please visit Java Data Structure and Algorithm Tutorial, Java Operation DOM Node Skills Summary, Java File and Directory Operation Skills Summary and Java Cache Operation Skills Summary.

I hope this article has been helpful in java programming.


Related articles: