Java Implementation of the method example for solving the n-degree polynomial, java polynomial example

Source: Internet
Author: User

Java Implementation of the method example for solving the n-degree polynomial, java polynomial example

The example in this article describes how Java implements the solution to a polynomial of n times. We will share this with you for your reference. The details are as follows:

Trend Prediction is required for the project. linear fitting, second-order curve fitting, and exponential fitting algorithms are used. Various linear fitting algorithms are written as matrices in this form:

X indicates the abscissa sample value, y indicates the ordinate sample value, I indicates the serial number of the sample point, a indicates the coefficient, N indicates the number of sample points, and n indicates the order, therefore, linear fitting finally becomes a problem for solving the higher order equations.

I don't know if there are any useful java matrix computing packages. I am not good at collecting such materials, so I had to pick up linear algebra that has been put down for many years, I wrote a java program using the augmented matrix algorithm to solve the high-order equations. Paste the Code directly:

Package commonAlgorithm; public class PolynomialSoluter {private double [] [] matrix; private double [] result; private int order; public PolynomialSoluter () {} // check the length of the input item 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-forward for (int I = 0; I <order; I ++) {// if the diagonal line of the current row is 0, exchange if (! SwithIfZero (I) return null; // decode 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 deyuan-reverse-removed redundant Calculation 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 swithIfZero (int I) {if (matrix [I] [I] = 0) {int j = I + 1; // locate the column where the position is not 0 while (j <order & matrix [j] [I] = 0) j ++; // if the corresponding position is all 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 ;}}

If you have better algorithms or a suitable matrix calculation package, please contact us.

PS: Here are some recommended computing tools for your reference:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.