The Power ClassExtending our least squares capability to include an equation of the form shown in Eq. (24.11) is quite simple. We have already done most of the work. The generic part of the analysis, the leastSquaresFit() method from the DataModeling class, doesn't need to be changed at all. All we have to do is to write a Polynomial subclass that will model Eq. (24.11). We will call this class the Power class. Its code listing is shown next . Eq. (24.11) is a third-order polynomial equation so the Power constructor simply calls the Polynomial constructor passing it the value 3. The only other thing the Power class must do is to override the loadArrays() and getValue() methods . The loadArrays() method is overridden according to Eq. (24.12) and Eq. (24.13). The getValue() method is overridden to return a value according to Eq. (24.10). package TechJava.MathLib; public class Power extends Polynomial { // This models the third-order equation // lnY = A*(lnT)^3 + B*(lnT)^2 + C*lnT + E public Power(){ super(3); } // Override the loadArray() method for a power // function. public void loadArrays(double x[], double y[]) { int numPoints = x.length; double order = getOrder(); double A, b; for(int j=0; j<order+1; ++j) { b = 0.0; for(int k=0; k<numPoints; ++k) { b += Math.pow(Math.log(x[k]),order j)*y[k]; } setB(j,b); } for(int i=0; i<order+1; ++i) { for(int j=0; j<order+1; ++j) { A = 0.0; for(int k=0; k<numPoints; ++k) { A += Math.pow(Math.log(x[k]),order - i)* Math.pow(Math.log(x[k]),order - j); } setA(i,j,A); } } } // Return a value along the least squares fit line. // This implementation is for a power function public double getValue(double T) { double tmp; double A, B, C, D; double lnT = Math.log(T); A = getCoefficient(0); B = getCoefficient(1); C = getCoefficient(2); D = Math.exp(getCoefficient(3)); tmp = A*lnT*lnT + B*lnT + C; return D*Math.pow(T,tmp); } } Table 24.2. Collision Integral Data
To test if the Power class is working properly, let's try to curve fit the collision integral data shown in Table 24.2. In this case the data is for the (2,2)* collision integral for the O 2 “ O 2 collision pair. Data will be used from two sources, 2 , 3 so there will be two data points at each of the temperature values. The PowerDemo class fits the data shown in Table 24.2 to an expression of the type shown in Eq. (24.10). The process is very similar to the specific heat curve fit example earlier in this chapter. Arrays named x[] and y[] are defined to store the data from Table 24.2. Since the modified curve fit expression shown in Eq. (24.11) uses the natural logarithm of the collision integral data, the elements of the y[] array are replaced with their natural logarithms. A Power object is created and sent along with the arrays x[] and y[] to the leastSquaresFit() method. The resulting coefficients are printed as well as a column of collision integral data calculated using the curve fit equation. import TechJava.MathLib.*; public class PowerDemo { public static void main(String args[]) { double x[] = { 200.0, 500.0, 1000.0, 2000.0, 3000.0, 4000.0, 5000.0, 6000.0, 200.0, 500.0, 1000.0, 2000.0, 3000.0, 4000.0, 5000.0, 6000.0 }; double y[] = { 10.892, 9.648, 9.723, 9.351, 8.945, 8.585, 8.269, 7.991, 14.914, 11.435, 9.877, 8.701, 8.076, 7.635, 7.289, 7.003 }; // replace the y[] array elements with their // natural logarithm for(int i=0; i<y.length; ++i) { y[i] = Math.log(y[i]); } // Create a Power object that represents the eqn // y = D*Math.pow(T, A*lnT*lnT + B*lnT + C) Power power = new Power(); // Perform the least squares fit DataModeling.leastSquaresFit(power, x, y); // Write out the power function coefficients // and some data values. Remember that the // D coefficient is computed as ln(D) System.out.println("A = " + power.getCoefficient(0)); System.out.println("B = " + power.getCoefficient(1)); System.out.println("C = " + power.getCoefficient(2)); System.out.println("D = " + Math.exp(power.getCoefficient(3))); System.out.println(); double T; for (int i=2; i<61; ++i) { T = 100*i; System.out.println("T = " + T + " Omega = " + power.getValue(T)); } } } Output ” A = -0.017596459962575077 B = 0.37349043457915776 C = -2.748620708686359 D = 10292.688325352558 Figure 24.2 shows a plot of the computed curve fit equation along with the initial data. You can see that there is a considerable amount of scatter between the two data sources. The least squares curve fit equation lies pretty much halfway between the two sets of original data. Figure 24.2. Curve fit to collision integral data
|