clear disp('Quadratic curve fit via least squares') A = [ 1 1 1;4 2 1;9 3 1;16 4 1;25 5 1] pause d = [1 5 8 17 26]' pause disp('Use the inverse matrix to solve the normal equations') sol1 = inv(A'*A)*(A'*d) pause disp('Use Gauss elimination via \ to solve the normal equations') sol2 = (A'*A)\(A'*d) pause disp('Use the QR factoriztion of A via A\d') sol3 = A\d pause disp('Compare the quadratic and linear approximation') disp('Linear approximatin via A(:,2:3)\d') pause sol_lin = A(:,2:3)\d sol_quad = A\d pause res_lin = (d - A(:,2:3)*sol_lin)'*(d - A(:,2:3)*sol_lin) res_quad = (d - A*sol_quad)'*(d - A*sol_quad) pause x = 1:1:5 pause ls_lin = sol_lin(1)*x + sol_lin(2) ls_quad = sol_quad(1)*x.^2 +sol_quad(2)*x + sol_quad(3) plot(x,d,'*',x,ls_lin,x, ls_quad)