Cody

# Problem 258. linear least squares fitting

Solution 1683439

Submitted on 30 Nov 2018
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Fail
x = [1,2,3,4]'; y = rand(4,1); f{1} = @(x) ones(size(x)); aref=mean(y); assert(norm(fit_coefficients(f,x,y)-aref)<1e-6)

a = 1 1 1 1 M = 1 1 1 1

Undefined function 'quadprog' for input arguments of type 'double'. Error in fit_coefficients (line 7) a = quadprog( 2*M'*M, -2*y'*M) Error in Test1 (line 5) assert(norm(fit_coefficients(f,x,y)-aref)<1e-6)

2   Fail
x = [1,2,3,4,5]' + randn(5,1); y = [1,2,3,4,5]' + randn(5,1); f{1} = @(x) ones(size(x)); f{2} = @(x) x; aref(2) = sum((x-mean(x)).*(y-mean(y)))/sum((x-mean(x)).^2); aref(1) = mean(y)-aref(2)*mean(x); assert(norm(fit_coefficients(f,x,y)-aref')<1e-6)

a = 1 0 1 0 1 0 1 0 1 0 a = 1.0000 -0.1106 1.0000 3.3133 1.0000 4.1448 1.0000 3.6559 1.0000 4.1390 M = 1.0000 -0.1106 1.0000 3.3133 1.0000 4.1448 1.0000 3.6559 1.0000 4.1390

Undefined function 'quadprog' for input arguments of type 'double'. Error in fit_coefficients (line 7) a = quadprog( 2*M'*M, -2*y'*M) Error in Test2 (line 7) assert(norm(fit_coefficients(f,x,y)-aref')<1e-6)

3   Fail
x = [1:15]' + randn(15,1); y = -10+0.2*x-0.5*x.^2+0.4*x.^3+0.001*log(abs(x)) + 0.2*randn(15,1); f{1} = @(x) ones(size(x)); f{2} = @(x) x; f{3} = @(x) x.^2; f{4} = @(x) x.^3; aref = fliplr(polyfit(x,y,3)); assert(norm(fit_coefficients(f,x,y)-aref')<1e-6)

a = 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 a = 1.0000 2.3370 0 0 1.0000 0.7847 0 0 1.0000 3.8274 0 0 1.0000 4.1016 0 0 1.0000 4.7431 0 0 1.0000 5.3302 0 0 1.0000 6.6428 0 0 1.0000 8.1219 0 0 1.0000 8.1868 0 0 1.0000 10.1459 0 0 1.0000 11.3998 0 0 1.0000 11.9402 0 0 1.0000 12.9459 0 0 1.0000 12.1814 0 0 1.0000 14.0505 0 0 a = 1.0000 2.3370 5.4617 0 1.0000 0.7847 0.6157 0 1.0000 3.8274 14.6488 0 1.0000 4.1016 16.8229 0 1.0000 4.7431 22.4968 0 1.0000 5.3302 28.4115 0 1.0000 6.6428 44.1266 0 1.0000 8.1219 65.9652 0 1.0000 8.1868 67.0235 0 1.0000 10.1459 102.9391 0 1.0000 11.3998 129.9556 0 1.0000 11.9402 142.5685 0 1.0000 12.9459 167.5971 0 1.0000 12.1814 148.3860 0 1.0000 14.0505 197.4169 0 a = 1.0e+03 * 0.0010 0.0023 0.0055 0.0128 0.0010 0.0008 0.0006 0.0005 0.0010 0.0038 0.0146 0.0561 0.0010 0.0041 0.0168 0.0690 0.0010 0.0047 0.0225 0.1067 0.0010 0.0053 0.0284 0.1514 0.0010 0.0066 0.0441 0.2931 0.0010 0.0081 0.0660 0.5358 0.0010 0.0082 0.0670 0.5487 0.0010 0.0101 0.1029 1.0444 0.0010 0.0114 0.1300 1.4815 0.0010 0.0119 0.1426 1.7023 0.0010 0.0129 0.1676 2.1697 0.0010 0.0122 0.1484 1.8075 0.0010 0.0141 0.1974 2.7738 M = 1.0e+03 * 0.0010 0.0023 0.0055 0.0128 0.0010 0.0008 0.0006 0.0005 0.0010 0.0038 0.0146 0.0561 0.0010 0.0041 0.0168 0.0690 0.0010 0.0047 0.0225 0.1067 0.0010 0.0053 0.0284 0.1514 0.0010 0.0066 0.0441 0.2931 0.0010 0.0081 0.0660 0.5358 0.0010 0.0082 0.0670 0.5487 0.0010 0.0101 0.1029 1.0444 0.0010 0.0114 0.1300 1.4815 0.0010 0.0119 0.1426 1.7023 0.0010 0.0129 0.1676 2.1697 0.0010 0.0122 0.1484 1.8075 0.0010 0.0141 0.1974 2.7738

Undefined function 'quadprog' for input arguments of type 'double'. Error in fit_coefficients (line 7) a = quadprog( 2*M'*M, -2*y'*M) Error in Test3 (line 8) assert(norm(fit_coefficients(f,x,y)-aref')<1e-6)

4   Fail
x = [0:0.1:2*pi]'; y = 0.123 + 0.456*sin(x).*exp(0.1*x); f{1} = @(x) ones(size(x)); f{2} = @(x) sin(x).*exp(0.1*x); aref=[0.123 0.456]'; assert(norm(fit_coefficients(f,x,y)-aref)<1e-6)

a = 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 a = 1.0000 0 1.0000 0.1008 1.0000 0.2027 1.0000 0.3045 1.0000 0.4053 1.0000 0.5040 1.0000 0.5996 1.0000 0.6909 1.0000 0.7771 1.0000 0.8571 1.0000 0.9300 1.0000 0.9948 1.0000 1.0509 1.0000 1.0973 1.0000 1.1335 1.0000 1.1589 1.0000 1.1730 1.0000 1.1754 1.0000 1.1659 1.0000 1.1443 1.0000 1.1106 1.0000 1.0649 1.0000 1.0074 1.0000 0.9385 1.0000 0.8587 1.0000 0.7685 1.0000 0.6686 1.0000 0.5599 1.0000 0.4432 1.0000 0.3197 1.0000 0.1905 1.0000 0.0567 1.0000 -0.0804 1.0000 -0.2194 1.0000 -0.3590 1.0000 -0.4978 1.0000 -0.6343 1.0000 -0.7671 1.0000 -0.8947 1.0000 -1.0158 1.0000 -1.1290 1.0000 -1.2330 1.0000 -1.3265 1.0000 -1.4084 1.0000 -1.4776 1.0000 -1.5331 1.0000 -1.5741 1.0000 -1.5999 1.0000 -1.6099 1.0000 -1.6037 1.0000 -1.5810 1.0000 -1.5418 1.0000 -1.4860 1.0000 -1.4140 1.0000 -1.3261 1.0000 -1.2229 1.0000 -1.1051 1.0000 -0.9738 1.0000 -0.8298 1.0000 -0.6745 1.0000 -0.5091 1.0000 -0.3353 1.0000 -0.1545 M = 1.0000 0 1.0000 0.1008 1.0000 0.2027 1.0000 0.3045 1.0000 0.4053 1.0000 0.5040 1.0000 0.5996 1.0000 0.6909 1.0000 0.7771 1.0000 0.8571 1.0000 0.9300 1.0000 0.9948 1.0000 1.0509 1.0000 1.0973 1.0000 1.1335 1.0000 1.1589 1.0000 1.1730 1.0000 1.1754 1.0000 1.1659 1.0000 1.1443 1.0000 1.1106 1.0000 1.0649 1.0000 1.0074 1.0000 0.9385 1.0000 0.8587 1.0000 0.7685 1.0000 0.6686 1.0000 0.5599 1.0000 0.4432 1.0000 0.3197 1.0000 0.1905 1.0000 0.0567 1.0000 -0.0804 1.0000 -0.2194 1.0000 -0.3590 1.0000 -0.4978 1.0000 -0.6343 1.0000 -0.7671 1.0000 -0.8947 1.0000 -1.0158 1.0000 -1.1290 1.0000 -1.2330 1.0000 -1.3265 1.0000 -1.4084 1.0000 -1.4776 1.0000 -1.5331 1.0000 -1.5741 1.0000 -1.5999 1.0000 -1.6099 1.0000 -1.6037 1.0000 -1.5810 1.0000 -1.5418 1.0000 -1.4860 1.0000 -1.4140 1.0000 -1.3261 1.0000 -1.2229 1.0000 -1.1051 1.0000 -0.9738 1.0000 -0.8298 1.0000 -0.6745 1.0000 -0.5091 1.0000 -0.3353 1.0000 -0.1545

Undefined function 'quadprog' for input arguments of type 'double'. Error in fit_coefficients (line 7) a = quadprog( 2*M'*M, -2*y'*M) Error in Test4 (line 6) assert(norm(fit_coefficients(f,x,y)-aref)<1e-6)