// lsfit_lect.java from lecture notes // compile javac -cp . lsfit_lect.java // run java -cp . lsfit_lect > lsfit_lect_java.out // just first order, linear fit class lsfit_lect { // fit x1 x2 x3 y y = a x1 + b x2 + c x3 double xd[][] = {{1.0, 2.5, 3.7}, // 32.5 {2.0, 2.5, 3.6}, // 7.2 {3.0, 2.7, 3.5}, // 6.9 {2.2, 2.1, 3.1}, // 22.4 {1.5, 2.0, 2.6}, // 10.4 {1.6, 2.0, 3.1}}; // 11.3 double yd[] = {32.5, 7.2, 6.9, 22.4, 10.4, 11.3}; // Y actual double A[][] = new double[3][3]; double X[] = new double[3]; double Y[] = new double[3]; // |A| * |X| = |Y| |X| will be a, b, c lsfit_lect() { System.out.println("lsfit_lect.java run to make lsfit_lect.out"); for(int i=0; i<3; i++) { Y[i] = 0.0; // must start sum with zero for(int k=0; k<6; k++) Y[i] += xd[k][i]*yd[k]; // Xi * Yk for(int j=0; j<3; j++) { A[i][j] = 0.0; // must start sum with zero for(int k=0; k<6; k++) // run through observations { A[i][j] += xd[k][j]*xd[k][i]; // Xj * Xi } } } // end i new simeq(A, Y, X); // pick correct argument list System.out.println("a="+X[0]+" b="+X[1]+" c="+X[2]); // check System.out.println("really bad fit"); for(int k=0; k<6; k++) // k is observation System.out.println("Y_actual="+yd[k]+" Y_approximate="+ (X[0]*xd[k][0]+X[1]*xd[k][1]+X[2]*xd[k][2])); } public static void main (String[] args) { new lsfit_lect(); // construct and execute } } // end lsfit_lect.java