This article describes the Java implementation method to solve a single n-degree polynomial. Share it for your reference, as follows:
The project needs to make trend predictions, using algorithms such as linear fitting, 2-order curve fitting and exponential fitting. Various linear fitting algorithms are written into matrices in this form:
Where x is the horizontal coordinate sampling value, y is the vertical coordinate sampling value, i is the sequence number of the sampling point, a is the coefficient, N is the number of the sampling points, and n is the order, so linear fitting finally turns into a problem of solving the system of higher-order equations.
I don’t know if there is any easy-to-use Java matrix operation package. I am not good at collecting this kind of information, so I had to pick up the linear algebra that I had put down for many years and wrote a Java program myself to use the algorithm of the augmented matrix to solve higher-order equations. Just paste the code:
package commonAlgorithm;public class PolynomialSoluter { private double[][] matrix; private double[] result; private int order; public PolynomialSoluter() { } // Check the input length and generate the augmented matrix private boolean init(double[][] matrixA, double[] arrayB) { order = arrayB.length; if (matrixA.length != order) return false; matrix = new double[order][order + 1]; for (int i = 0; i < order; i++) { if (matrixA[i].length != order) return false; for (int j = 0; j < order; j++) { matrix[i][j] = matrixA[i][j]; } matrix[i][order] = arrayB[i]; } result = new double[order]; return true; } public double[] getResult(double[][] matrixA, double[] arrayB) { if (!init(matrixA, arrayB)) return null; // Gaussian elimination-for (int i = 0; i < order; i++) { // If the diagonal term of the current row is 0, it will be exchanged with the row with the same column of the following rows that are not 0 if (!swithIfZero(i)) return null; // Elimination for (int j = i + 1; j < order; j++) { if (matrix[j][i] == 0) continue; double factor = matrix[j][i] / matrix[i][i]; for (int l = i; l < order + 1; l++) matrix[j][l] = matrix[j][l] - matrix[i][l] * factor; } } // Gaussian Elimination-Reverse-Remote-Remote redundant calculations for (int i = order - 1; i >= 0; i--) { result[i] = matrix[i][order] / matrix[i][i]; for (int j = i - 1; j > -1; j--) matrix[j][order] = matrix[j][order] - result[i] * matrix[j][i]; } return result; } private boolean swethIfZero(int i) { if (matrix[i][i] == 0) { int j = i + 1; // Find the column that is not zero at the corresponding position while (j < order && matrix[j][i] == 0) j++; // If all corresponding positions are 0, there is no solution if (j == order) return false; else switchRows(i, j); } return true; } private void switchRows(int i, int j) { double[] tmp = matrix[i]; matrix[i] = matrix[j]; matrix[j] = tmp; }}Welcome to communicate if there is a better algorithm or a suitable matrix operation package.
PS: Here are a few calculation tools for you to refer to:
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.