Cody

# Problem 258. linear least squares fitting

Solution 1683438

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

Undefined function or variable 'M'. Error in fit_coefficients (line 6) 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 1.5442 1.0000 3.0079 1.0000 4.2787 1.0000 3.7110 1.0000 6.6574

Undefined function or variable 'M'. Error in fit_coefficients (line 6) 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 1.6264 0 0 1.0000 1.1244 0 0 1.0000 3.4729 0 0 1.0000 4.1347 0 0 1.0000 4.2839 0 0 1.0000 6.2264 0 0 1.0000 6.8593 0 0 1.0000 7.4992 0 0 1.0000 8.8220 0 0 1.0000 9.9282 0 0 1.0000 12.0223 0 0 1.0000 10.8987 0 0 1.0000 13.5325 0 0 1.0000 11.2739 0 0 1.0000 13.8008 0 0 a = 1.0000 1.6264 2.6450 0 1.0000 1.1244 1.2642 0 1.0000 3.4729 12.0609 0 1.0000 4.1347 17.0956 0 1.0000 4.2839 18.3520 0 1.0000 6.2264 38.7675 0 1.0000 6.8593 47.0503 0 1.0000 7.4992 56.2374 0 1.0000 8.8220 77.8273 0 1.0000 9.9282 98.5700 0 1.0000 12.0223 144.5365 0 1.0000 10.8987 118.7824 0 1.0000 13.5325 183.1284 0 1.0000 11.2739 127.1009 0 1.0000 13.8008 190.4629 0 a = 1.0e+03 * 0.0010 0.0016 0.0026 0.0043 0.0010 0.0011 0.0013 0.0014 0.0010 0.0035 0.0121 0.0419 0.0010 0.0041 0.0171 0.0707 0.0010 0.0043 0.0184 0.0786 0.0010 0.0062 0.0388 0.2414 0.0010 0.0069 0.0471 0.3227 0.0010 0.0075 0.0562 0.4217 0.0010 0.0088 0.0778 0.6866 0.0010 0.0099 0.0986 0.9786 0.0010 0.0120 0.1445 1.7377 0.0010 0.0109 0.1188 1.2946 0.0010 0.0135 0.1831 2.4782 0.0010 0.0113 0.1271 1.4329 0.0010 0.0138 0.1905 2.6285

Undefined function or variable 'M'. Error in fit_coefficients (line 6) 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

Undefined function or variable 'M'. Error in fit_coefficients (line 6) a = quadprog( 2*M'*M, -2*y'*M) Error in Test4 (line 6) assert(norm(fit_coefficients(f,x,y)-aref)<1e-6)