Example: Least Squares Fit


To demonstrate the process of developing a generic class library, let's use the example of implementing a least squares curve fit to a collection of data. We'll go over the details of the least squares fit methodology in the next chapter, but the general objective is to come up with a polynomial expression that models a collection of data such that the sum of the square of the errors at each data point is minimized. The procedure comes down to solving the matrix equation shown in Eq. (23.1).

Equation 23.1

graphics/23equ01.gif


The x vector contains the coefficients of the curve fit polynomial equation. The b vector contains the data being fitted. The process of solving a system of equations is, of course, generic, but the contents and size of the A, b , and x arrays are problem-specific. Since the analysis can be broken up into generic and problem-specific components , the least squares fit functionality can be built into a generic class library.

There are three generic parts to the least squares fit analysis ”the arrays must be loaded with the appropriate data, the system of equations shown in Eq. (23.1) is solved , and the polynomial coefficients are returned. The generic elements of the analysis will be placed in a method named leastSquaresFit() that is defined in the DataModeling class.

The problem-specific elements of the least squares fit process are the form of the curve fit polynomial equation, the data that will be modeled , and the size and contents of the arrays. This information, and the methods used to access it, is stored in a class named Polynomial . The Polynomial class will also take care of filling the arrays A T A and A T b . An instance of the Polynomial class, or one of its subclasses, is passed to the leastSquaresFit() method as an argument.



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