Cody

# Problem 258. linear least squares fitting

Solution 1683440

Submitted on 30 Nov 2018 by Martin C.
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   Pass
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 a = 0.2597

2   Pass
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.0531 1.0000 5.0078 1.0000 4.0944 1.0000 4.6863 1.0000 4.9418 M = 1.0000 -0.0531 1.0000 5.0078 1.0000 4.0944 1.0000 4.6863 1.0000 4.9418 a = 1.7284 0.1805

3   Pass
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 -0.0751 0 0 1.0000 1.4609 0 0 1.0000 2.2656 0 0 1.0000 5.2210 0 0 1.0000 3.3636 0 0 1.0000 4.9409 0 0 1.0000 4.7048 0 0 1.0000 6.5898 0 0 1.0000 9.0594 0 0 1.0000 9.8503 0 0 1.0000 10.9882 0 0 1.0000 12.3727 0 0 1.0000 13.5321 0 0 1.0000 12.4314 0 0 1.0000 16.2072 0 0 a = 1.0000 -0.0751 0.0056 0 1.0000 1.4609 2.1341 0 1.0000 2.2656 5.1329 0 1.0000 5.2210 27.2590 0 1.0000 3.3636 11.3141 0 1.0000 4.9409 24.4121 0 1.0000 4.7048 22.1350 0 1.0000 6.5898 43.4257 0 1.0000 9.0594 82.0722 0 1.0000 9.8503 97.0275 0 1.0000 10.9882 120.7415 0 1.0000 12.3727 153.0834 0 1.0000 13.5321 183.1173 0 1.0000 12.4314 154.5405 0 1.0000 16.2072 262.6730 0 a = 1.0e+03 * 0.0010 -0.0001 0.0000 -0.0000 0.0010 0.0015 0.0021 0.0031 0.0010 0.0023 0.0051 0.0116 0.0010 0.0052 0.0273 0.1423 0.0010 0.0034 0.0113 0.0381 0.0010 0.0049 0.0244 0.1206 0.0010 0.0047 0.0221 0.1041 0.0010 0.0066 0.0434 0.2862 0.0010 0.0091 0.0821 0.7435 0.0010 0.0099 0.0970 0.9557 0.0010 0.0110 0.1207 1.3267 0.0010 0.0124 0.1531 1.8941 0.0010 0.0135 0.1831 2.4780 0.0010 0.0124 0.1545 1.9212 0.0010 0.0162 0.2627 4.2572 M = 1.0e+03 * 0.0010 -0.0001 0.0000 -0.0000 0.0010 0.0015 0.0021 0.0031 0.0010 0.0023 0.0051 0.0116 0.0010 0.0052 0.0273 0.1423 0.0010 0.0034 0.0113 0.0381 0.0010 0.0049 0.0244 0.1206 0.0010 0.0047 0.0221 0.1041 0.0010 0.0066 0.0434 0.2862 0.0010 0.0091 0.0821 0.7435 0.0010 0.0099 0.0970 0.9557 0.0010 0.0110 0.1207 1.3267 0.0010 0.0124 0.1531 1.8941 0.0010 0.0135 0.1831 2.4780 0.0010 0.0124 0.1545 1.9212 0.0010 0.0162 0.2627 4.2572 a = -9.8268 0.0969 -0.4854 0.3994

4   Pass
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 a = 0.1230 0.4560