/* Programming template for Final Part 2 Question 1 Refer to Section 4.1.4 see also Exercise 4.4 from the text. Given points x[i] and y[i] in R^2, please complete the subroutine findAb below to find the 2x2 matrix A and the vector b in R^2 which minimizes the sum sum || A x[i] + b - y[i] ||_2^2 Feel free to use the matrix library and QR decomposition subroutines developed in class and any other code that you find useful as part of your program. Note that the correct answer is given by Agold and bgold below. */ #include #include #include #define N 32 double x[N][2] = { {8.164057, 1.877290}, {4.249813, 6.239451}, {1.532861, 9.439875}, {1.928936, 9.957978}, {8.432896, 2.509255}, {3.611497, 0.505932}, {4.728512, 2.105011}, {2.428723, 4.497075}, {6.052792, 5.567077}, {4.506568, 3.340023}, {9.056291, 6.688562}, {5.355657, 4.325314}, {2.973905, 8.169642}, {7.782521, 2.545538}, {4.607688, 1.125653}, {9.662162, 2.771745}, {3.002943, 3.911975}, {9.011196, 4.535804}, {3.351850, 0.940132}, {4.493782, 1.784746}, {3.449387, 8.105279}, {2.290678, 8.177899}, {0.210289, 4.719401}, {2.674974, 6.263082}, {0.286478, 7.181543}, {9.603105, 9.342769}, {3.870104, 4.958762}, {3.668083, 6.844010}, {3.128405, 1.450603}, {9.389548, 7.736092}, {2.576257, 9.051710}, {0.507837, 5.579200}}; double y[N][2] = { {-3.139679, 2.135419}, {-2.936915, -0.543049}, {-2.812507, -2.280058}, {-2.801842, -2.348928}, {-3.551787, 1.991059}, {-0.836936, 1.446066}, {-1.735525, 1.032166}, {-1.520510, -0.359967}, {-3.395512, 0.271135}, {-2.047350, 0.531590}, {-4.994850, 0.792345}, {-2.950679, 0.458651}, {-2.892620, -1.320301}, {-3.346844, 1.746298}, {-1.412302, 1.329627}, {-4.152725, 2.233632}, {-1.502362, -0.045263}, {-4.265256, 1.456283}, {-0.925103, 1.147204}, {-1.669033, 1.030497}, {-2.985123, -1.232566}, {-2.561966, -1.865504}, {-0.760914, -0.929590}, {-2.122985, -1.023203}, {-1.316517, -1.888057}, {-6.177205, 0.092818}, {-2.291222, -0.167101}, {-2.879485, -0.866149}, {-0.947220, 0.858958}, {-5.735077, 0.584227}, {-2.845794, -1.824628}, {-0.963418, -1.255478}}; double Agold[2][2]={ {-4.37539407886573e-01, -3.02028091780604e-01}, {2.82916960758559e-01, -3.34980192536044e-01}}; double bgold[2]={8.95785381554899e-01, 4.39543855327825e-01}; double A[2][2],b[2]; // Find the matrix A and vector b such that Ax+b best approximates y // for all vectors x[i] and y[i]. void findAb(int n,double x[n][2],double y[n][2], double A[2][2],double b[2]){ // Fill in code here to solve for A and b. } int main(){ printf("Final Part 2 Question 1.\n"); findAb(N,x,y,A,b); // At this point A should equal Agold and b equal bgold. printf("A=\n"); for(int i=0;i<2;i++) printf("%g %g\n",A[i][0],A[i][1]); printf("b=\n%g\n%g\n",b[0],b[1]); double r=0; for(int i=0;i<2;i++) { for(int j=0;j<2;j++){ double t=A[i][j]-Agold[i][j]; r+=t*t; } double t=b[i]-bgold[i]; r+=t*t; } r=sqrt(r); printf("sqrt(|A-Agold|_frob^2+|b-bgold|_2^2)=%g\n",r); if(r<1e-7){ printf("Correct! A and b agree with Agold and bgold!\n"); } else { printf("Incorrect! Estimates for A and b are wrong!\n"); } return 0; }