Cody

# Problem 258. linear least squares fitting

Solution 2220066

Submitted on 19 Apr 2020 by Jerry Wright
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 = 0 0 0 0 a = 1 1 1 1 M = 1 1 1 1 a = 0.2733

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 = 0 0 0 0 0 0 0 0 0 0 a = 1 0 1 0 1 0 1 0 1 0 a = 1.0000 1.1570 1.0000 2.1460 1.0000 3.6960 1.0000 3.3669 1.0000 5.2675 M = 1.0000 1.1570 1.0000 2.1460 1.0000 3.6960 1.0000 3.3669 1.0000 5.2675 a = -0.2554 0.8507

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 = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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.2079 0 0 1.0000 2.5915 0 0 1.0000 2.5258 0 0 1.0000 5.0300 0 0 1.0000 4.3317 0 0 1.0000 5.4323 0 0 1.0000 7.5605 0 0 1.0000 8.6759 0 0 1.0000 9.5714 0 0 1.0000 10.2016 0 0 1.0000 12.7171 0 0 1.0000 11.9496 0 0 1.0000 11.7126 0 0 1.0000 13.3294 0 0 1.0000 15.3305 0 0 a = 1.0000 -0.2079 0.0432 0 1.0000 2.5915 6.7159 0 1.0000 2.5258 6.3797 0 1.0000 5.0300 25.3014 0 1.0000 4.3317 18.7635 0 1.0000 5.4323 29.5099 0 1.0000 7.5605 57.1609 0 1.0000 8.6759 75.2704 0 1.0000 9.5714 91.6121 0 1.0000 10.2016 104.0730 0 1.0000 12.7171 161.7252 0 1.0000 11.9496 142.7935 0 1.0000 11.7126 137.1853 0 1.0000 13.3294 177.6730 0 1.0000 15.3305 235.0242 0 a = 1.0e+03 * 0.0010 -0.0002 0.0000 -0.0000 0.0010 0.0026 0.0067 0.0174 0.0010 0.0025 0.0064 0.0161 0.0010 0.0050 0.0253 0.1273 0.0010 0.0043 0.0188 0.0813 0.0010 0.0054 0.0295 0.1603 0.0010 0.0076 0.0572 0.4322 0.0010 0.0087 0.0753 0.6530 0.0010 0.0096 0.0916 0.8769 0.0010 0.0102 0.1041 1.0617 0.0010 0.0127 0.1617 2.0567 0.0010 0.0119 0.1428 1.7063 0.0010 0.0117 0.1372 1.6068 0.0010 0.0133 0.1777 2.3683 0.0010 0.0153 0.2350 3.6030 M = 1.0e+03 * 0.0010 -0.0002 0.0000 -0.0000 0.0010 0.0026 0.0067 0.0174 0.0010 0.0025 0.0064 0.0161 0.0010 0.0050 0.0253 0.1273 0.0010 0.0043 0.0188 0.0813 0.0010 0.0054 0.0295 0.1603 0.0010 0.0076 0.0572 0.4322 0.0010 0.0087 0.0753 0.6530 0.0010 0.0096 0.0916 0.8769 0.0010 0.0102 0.1041 1.0617 0.0010 0.0127 0.1617 2.0567 0.0010 0.0119 0.1428 1.7063 0.0010 0.0117 0.1372 1.6068 0.0010 0.0133 0.1777 2.3683 0.0010 0.0153 0.2350 3.6030 a = -9.9862 0.2042 -0.4985 0.3998

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 = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!