The Polynomial Class


The Polynomial Class

The leastSquaresFit() method is generic and can be applied to any type of polynomial expression for any collection of data. Its main purpose is to solve the least squares system of equations. The problem-specific components of the analysis are passed to the method as input arguments. One of the input arguments is an instance of the Polynomial class. The Polynomial class code listing is shown in Chapter 23. It is somewhat long and won't be repeated in its entirety here, but we will review some of its more important elements.

The Polynomial class represents a general polynomial. An integer argument passed to the Polynomial class constructor specifies the order of the polynomial expression. If the number 1 is passed as an argument to the constructor the Polynomial represents a straight line. If the number 2 is passed, the Polynomial represents a quadratic equation, and so on.

In addition to storing the order of the polynomial it represents, the Polynomial class defines three other data members . The A[] [] array stores the values of the A T A matrix. The b[] array stores the values of the A T y matrix. The coeffs[] array is used to store the coefficients of the polynomial equation once they are computed.

The loadArrays() method is used to fill the A[] [] and b[] array elements following the expressions shown in Eq. (24.8) and Eq. (24.9). The getValue() method is used to return a value of the polynomial equation. The loadArrays() and getValue() method code listings are shown here.

 //  Load the A[][] and b[] arrays. This implementation //  is for a general polynomial expression public void loadArrays(double x[], double y[]) {   int numPoints = x.length;   for(int j=0; j<order+1; ++j) {     b[j] = 0.0;     for(int k=0; k<numPoints; ++k) {       b[j] += Math.pow(x[k],order-j)*y[k];     }   }   for(int i=0; i<order+1; ++i) {     for(int j=0; j<order+1; ++j) {       A[i][j] = 0.0;       for(int k=0; k<numPoints; ++k) {         A[i][j] += Math.pow(x[k],2.0*order-i-j);       }     }   } } //  Return a value along the least squares fit line. //  This implementation is for a general polynomial //  expression. public double getValue(double x) {   double y = 0.0;   for(int i=0; i<getOrder()+1; ++i) {     y += coeff[i]*Math.pow(x,order-i);   }   return y; } 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net