// lsfit_lect.c from lecture notes // compile gcc -o lsfit_lect lsfit_lect.c simeq.c -lm // run lsfit_lect > lsfit_lect_c.out #include "simeq.h" // needed to solve simultaneous equations #include #include int main(int argc, char* argv[]) { double xd[6][3] = {{1.0, 2.5, 3.7}, // 1 {2.0, 2.5, 3.6}, // 2 {3.0, 2.7, 3.5}, // 3 {2.2, 2.1, 3.1}, // 4 {1.5, 2.0, 2.6}, // 5 {1.6, 2.0, 3.1}}; // 6 double yd[6] = {32.5, 7.2, 6.9, 22.4, 10.4, 11.3}; // Y actual double A[3][3]; double X[3]; double Y[3]; // |A| * |X| = |Y| |X| will be a, b, c int i, j, k; printf("lsfit_lect.c run to make lsfit_lect_c.out \n"); for(i=0; i<3; i++) { Y[i] = 0.0; // must start sum with zero for(k=0; k<6; k++) Y[i] += xd[k][i]*yd[k]; // Xi * Yk for(j=0; j<3; j++) { A[i][j] = 0.0; // must start sum with zero for(k=0; k<6; k++) // run through observations { A[i][j] += xd[k][j]*xd[k][i]; // Xj * Xi } } } // end i simeq2(3, A, Y, X); // pick correct argument list printf("a=%f, b=%f, c=%f \n", X[0], X[1], X[2]); // check printf("really bad fit \n"); for(k=0; k<6; k++) // k is observation printf("Y_actual=%f, Y_approximate=%f \n", yd[k], X[0]*xd[k][0]+X[1]*xd[k][1]+X[2]*xd[k][2]); return 0; } // end lsfit_lect.c