Cody

# Problem 258. linear least squares fitting

Solution 2094478

Submitted on 16 Jan 2020 by Nico Noordam
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)

in = 1 1 1 1 int = 1 1 1 1 ans = 0.2777

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)

in = 1 1 1 1 1 in = 1.0000 0.4363 1.0000 1.3741 1.0000 1.8886 1.0000 2.5159 1.0000 3.6432 int = 1.0000 1.0000 1.0000 1.0000 1.0000 0.4363 1.3741 1.8886 2.5159 3.6432 ans = 3.1196 0.0544

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)

in = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 in = 1.0000 1.1506 1.0000 1.2167 1.0000 4.3887 1.0000 4.8986 1.0000 5.8129 1.0000 6.0442 1.0000 7.0647 1.0000 7.9015 1.0000 7.9629 1.0000 8.3276 1.0000 11.8613 1.0000 13.4443 1.0000 12.7704 1.0000 13.3745 1.0000 13.3613 in = 1.0000 1.1506 1.3239 1.0000 1.2167 1.4802 1.0000 4.3887 19.2604 1.0000 4.8986 23.9963 1.0000 5.8129 33.7895 1.0000 6.0442 36.5319 1.0000 7.0647 49.9099 1.0000 7.9015 62.4341 1.0000 7.9629 63.4085 1.0000 8.3276 69.3493 1.0000 11.8613 140.6912 1.0000 13.4443 180.7483 1.0000 12.7704 163.0822 1.0000 13.3745 178.8785 1.0000 13.3613 178.5244 in = 1.0e+03 * 0.0010 0.0012 0.0013 0.0015 0.0010 0.0012 0.0015 0.0018 0.0010 0.0044 0.0193 0.0845 0.0010 0.0049 0.0240 0.1175 0.0010 0.0058 0.0338 0.1964 0.0010 0.0060 0.0365 0.2208 0.0010 0.0071 0.0499 0.3526 0.0010 0.0079 0.0624 0.4933 0.0010 0.0080 0.0634 0.5049 0.0010 0.0083 0.0693 0.5775 0.0010 0.0119 0.1407 1.6688 0.0010 0.0134 0.1807 2.4300 0.0010 0.0128 0.1631 2.0826 0.0010 0.0134 0.1789 2.3924 0.0010 0.0134 0.1785 2.3853 int = 1.0e+03 * 0.0010 0.0010 0.0010 0.0010 0.0010 0.0010 0.0010 0.0010 0.0010 0.0010 0.0010 0.0010 0.0010 0.0010 0.0010 0.0012 0.0012 0.0044 0.0049 0.0058 0.0060 0.0071 0.0079 0.0080 0.0083 0.0119 0.0134 0.0128 0.0134 0.0134 0.0013 0.0015 0.0193 0.0240 0.0338 0.0365 0.0499 0.0624 0.0634 0.0693 0.1407 0.1807 0.1631 0.1789 0.1785 0.0015 0.0018 0.0845 0.1175 0.1964 0.2208 0.3526 0.4933 0.5049 0.5775 1.6688 2.4300 2.0826 2.3924 2.3853 ans = -9.9096 0.0773 -0.4755 0.3988

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)

in = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 in = 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 int = Columns 1 through 18 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0 0.1008 0.2027 0.3045 0.4053 0.5040 0.5996 0.6909 0.7771 0.8571 0.9300 0.9948 1.0509 1.0973 1.1335 1.1589 1.1730 1.1754 Columns 19 through 36 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.1659 1.1443 1.1106 1.0649 1.0074 0.9385 0.8587 0.7685 0.6686 0.5599 0.4432 0.3197 0.1905 0.0567 -0.0804 -0.2194 -0.3590 -0.4978 Columns 37 through 54 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 -0.6343 -0.7671 -0.8947 -1.0158 -1.1290 -1.2330 -1.3265 -1.4084 -1.4776 -1.5331 -1.5741 -1.5999 -1.6099 -1.6037 -1.5810 -1.5418 -1.4860 -1.4140 Columns 55 through 63 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 -1.3261 -1.2229 -1.1051 -0.9738 -0.8298 -0.6745 -0.5091 -0.3353 -0.1545 ans = 0.1230 0.4560