regression

regression - function regression global x ynoisy % First do...

This preview shows pages 1–2. Sign up to view the full content.

Sheet1 Page 1 function regression global x ynoisy % First do a linear regression example % Assume y = a1+a2*x+a3*x2+a4*cos(x)+a5*sin(x)+a6*cos(2*x)+a7*sin(2*x) fprintf('Linear regression example solved using linear least squares . . .\n\n') coefs = [3 a1 = coefs(1,1) a2 = coefs(2,1) a3 = coefs(3,1) a4 = coefs(4,1) a5 = coefs(5,1) a6 = coefs(6,1) a7 = coefs(7,1) x = linspace(0,100,101)' x2 = x.*x x3 = x2.*x cosx = cos(x) sinx = sin(x) cos2x = cos(2*x) sin2x = sin(2*x) y = a1+a2*x+a3*x2+a4*cosx+a5*sinx+a6*cos2x+a7*sin2x %Add noise to original data noisemag = 20 ynoisy = y+noisemag*(2*rand(101,1)-1) plot(x,y,'k-',x,ynoisy,'kx') legend('Original','Noisy') title('Linear regression sample points') pause A = [ones(101,1) x x2 cosx sinx cos2x sin2x] b = ynoisy coefs = A\b a1 = coefs(1,1) a2 = coefs(2,1) a3 = coefs(3,1) a4 = coefs(4,1) a5 = coefs(5,1) a6 = coefs(6,1) a7 = coefs(7,1) % Compute fitted value of y yfit = a1+a2*x+a3*x2+a4*cosx+a5*sinx+a6*cos2x+a7*sin2x plot(x,yfit,'k-',x,ynoisy,'kx') legend('Fitted','Noisy')

This preview has intentionally blurred sections. Sign up to view the full version.

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

regression - function regression global x ynoisy % First do...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online