ECOR 2606 - Lecture 17

Y za t y t y y t za a t z t y a t z

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 0Ty Matlab: [Q0, R0] =qr (Z, 0); % the optional 0 gives only the required parts of Q and R a = R0\(Q0’ *y); 3 3/10/2010 General least squares and left division: Assuming that n &gt; m + 1 (more points than functions) Z a = y is overdetermined Number of equations is greater than number of unknowns No unique solution Insuch cases the left division operator produces a “best fit” (least squares) solution Once Z has been created a can be found directly using left division: a = Z \ y; % fit curve to data points Matlab uses QR factorization to find the least squares solution Example: x = [ ‐8.0 ‐6.0 ‐4.0 ‐2.0 0 2.0 4.0 6.0 8.0]'; y = [ ‐817.5 ‐279.9 ‐139.4 ‐41.6 ‐23.8 ‐8.7 36.0 158.1 339.4 ]'; We want to fit a curve of the form y = a0x3+a1x2+a2x+a3 This is polynomial regression: p = polyfit (x, y, 3); % p is 1.1105 ‐3.2687 0.2947 0.7874 f = @(x) p(1) * x .^ 3 + p(2) * x .^ 2 + p(3) * x + p(4); r = correlate (x, y, f); % r is 0.9936 4 3/10/2010 We can also use general least squares regression with z0(x) =x3, z1(x) =x2, z2(x) =x, z3(x) =1 Creating Z: Z = zeros (length(x), 4); % pre allocate for efficiency for k = 1 : length(x) Z(k, 1) = x(k)^3; Z(k, 2) = x(k)^2; Z(k, 3) = x(k); Z(k, 4) = 1; end Using normal equations: Zt = Z'; a = (Zt* Z) \ (Zt * y) % solve Zt Z a = Zt y % a’ is 1.1105 ‐3.2687 0.2947 0.7874 Normal equations perhaps not a very good idea here (although the answer is OK): c = cond(Zt* Z); % c is 1.5999e+005, the matrix is very ill conditioned Using QR decomposition: [Q0, R0] = qr(Z, 0); a = R0 \ (Q0' * y); % a’ is 1.1105 ‐3.2687 0.2947 0.7874 Using left division: a = Z \ y; % a’ is 1.1105 ‐3.2687 0.2947 0.7874 5 3/10/2010 Data points and fitted curve: 6...
View Full Document

This note was uploaded on 09/13/2013 for the course ECOR 2606 taught by Professor Goheen during the Fall '10 term at Carleton CA.

Ask a homework question - tutors are online