Least-Squares Graphing Applet

 This Java/Swing applet allows a user to drag in x, y data from a spreadsheet and then perform least-squares polynomial curve fitting to the data.This applet can no longer be executed via this site, sorry.Here is a screenshot of the component/applet: Below is the single-threaded source code for the method that computes the least-squares coefficient matrix and vector and then makes calls to the JAMA matrix library.     private Polynomial doPolynomialLsFit( final double x [], final double y [], final int degree ) {                 // Build matrix of zeros for summation below         double[][] data = new double [ degree + 1 ] [ degree + 1 ];         for ( int i = 0; i < degree + 1; i++ ) {             for ( int j = 0; j < degree + 1; j++ )                 data [ i ][ j ] = 0.0;         }                 // Compute least squares matrix.         // This approach ignores the fact that this matrix is symmetric and only 1/2 needs to be         // computed because the remainder can just be assigned from mirror positions opposite        // the diagonal but this is easy to write/read and we're not crunching millions of data points.         // Also, significantly more efficient algorithms exist for raising numbers to a power than        // Math.pow(), but for this sort of "calculator" application, Math.pow() is perfectly adequate.         for ( int i = 0; i < degree + 1; i++ ) {             for ( int j = 0; j  < degree + 1; j++ ) {                 for ( int k = 0; k < x.length; k++ ) {                     data[ i ][ j ] += Math.pow( x[ k ], i + j );                 }             }         }                 //  Compute the "B" vector         double b [] = new double [ degree + 1 ];         for ( int i = 0; i < degree + 1; i++ ) {             double sum = 0.0;             for ( int j = 0; j < x.length; j++ ) {                 sum += y[ j ] * Math.pow( x[ j ], i );             }             b[ i ] = sum;         }                    // Instantiate Matrix objects         Matrix A = new Matrix( data, degree + 1, degree + 1 );         Matrix B = new Matrix( b, degree + 1 );         Matrix X = null;                 // Try to solve linear system of equations - it might fail if matrix is ill-conditioned         try {             X = A.solve( B );         }         catch ( RuntimeException re ) {             JOptionPane.showMessageDialog( this, "Error encountered when calling solve() on generated matrix!  Try lower degree fit.", "Solve Matrix Error", JOptionPane.ERROR_MESSAGE );             return null;         }                 // Extract resulting X vector coefficients into a double array         double [] c = new double [ degree + 1 ];         for ( int i = 0; i < degree + 1; i++ )             c[ i ] = X.get( i, 0 );                 // Instantiate a Polynomial object (which wraps a polynomial) and return it         Polynomial p = new Polynomial( degree, c );         return p;     }Copyright (c) Richard Creamer 2008 - 2010 - All Rights Reserved