Implementing the Generic Part


We will define the generic part of the least squares fit analysis in a class called DataModeling . The leastSquaresFit() method declared in the DataModeling class will be written to implement the generic part of the least squares fit process, which is essentially defining and solving a system of equations. Before you can solve the system of equations, the arrays must be loaded with the appropriate data. Before you fill the arrays you need two things ”the data that will be fit and the equation to which the data will be fit.

The leastSquaresFit() method takes three arguments. The data to be modeled is passed to the method in two arrays, x[] and y[] . Information about the curve fit polynomial equation is encapsulated in an instance of the Polynomial class or by an instance of a Polynomial subclass. We'll discuss and define the Polynomial class in the next section.

The arrays A T A and A T b are filled by having the Polynomial object call its loadArrays() method. Once the arrays are loaded with data, the gaussian() method from the EqnSolver class solves the system of equations. When the solution process is finished, the A T b vector is overwritten to contain the coefficients for the curve fit equation. The coefficients are copied into the Polynomial object's coeffs[] data member using the setCoefficients() method.

The DataModeling class source code is shown here.

 package TechJava.MathLib; public class DataModeling {   public static void leastSquaresFit(        Polynomial equation, double x[], double y[]) {     //  Load the AT*A and AT*b arrays using the data in     //  x[] and y[].     equation.loadArrays(x,y);     //  Solve the system of equations AT*A*x = AT*b     //  The b[] array is overwritten to contain the     //  curve fit coefficients.     EqnSolver.gaussian(equation.getA(),                        equation.getB());     //  Copy the curve fit coefficient values into     //  the Polynomial object's coeffs[] array.     equation.setCoefficients(equation.getB());   } } 


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