// "Java Tech"
// Code provided with book for educational purposes only.
// No warranty or guarantee implied.
// This code freely available. No copyright claimed.
// 2003
import java.awt.*;
/**
* Fit straight line to a set of data points.
* Implements the Fit interface.
*
**/
public class FitLine extends Fit
{
/**
* Use the Least Squares fit method for fitting a
* straight line to 2-D data for measurements
* y[i] vs. dependent variable x[i]. This fit assumes
* there are errors only on the y measuresments as
* given by the sigma_y array.
* See, e.g. Press et al., "Numerical Recipes..." for details
* of the algorithm.
**/
public static void fit (double [] parameters, double [] x, double [] y,
double [] sigma_x, double [] sigma_y, int num_points){
double s=0.0,sx=0.0,sy=0.0,sxx=0.0,sxy=0.0, del;
// Null sigma_y implies a constant error which drops
// out of the divisions of the sums.
if (sigma_y != null){
for (int i=0; i < num_points; i++){
s += 1.0/ (sigma_y[i]*sigma_y[i]);
sx += x[i]/ (sigma_y[i]*sigma_y[i]);
sy += y[i]/ (sigma_y[i]*sigma_y[i]);
sxx += (x[i]*x[i])/ (sigma_y[i]*sigma_y[i]);
sxy += (x[i]*y[i])/ (sigma_y[i]*sigma_y[i]);
}
} else {
s = x.length;
for (int i=0; i < num_points; i++){
sx += x[i];
sy += y[i];
sxx += x[i]*x[i];
sxy += x[i]*y[i];
}
}
del = s*sxx - sx*sx;
// Intercept
parameters[0] = (sxx*sy -sx*sxy)/del;
// Slope
parameters[1] = (s*sxy -sx*sy)/del;
// Errors (sd**2) on the:
// intercept
parameters[2] = sxx/del;
// and slope
parameters[3] = s/del;
} // fit
} // FitLine