Cody

Problem 258. linear least squares fitting

Solution 2094485

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 ans = 0.4911

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 1.2592 1.0000 1.8534 1.0000 4.2661 1.0000 4.3373 1.0000 2.9354 ans = 2.2791 0.3034

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 0.2207 1.0000 0.6752 1.0000 3.3457 1.0000 4.2672 1.0000 4.6797 1.0000 5.8056 1.0000 5.9620 1.0000 8.0862 1.0000 9.2001 1.0000 9.1224 1.0000 10.1921 1.0000 11.0629 1.0000 13.8606 1.0000 13.7241 1.0000 15.8954 in = 1.0000 0.2207 0.0487 1.0000 0.6752 0.4559 1.0000 3.3457 11.1940 1.0000 4.2672 18.2093 1.0000 4.6797 21.8999 1.0000 5.8056 33.7049 1.0000 5.9620 35.5458 1.0000 8.0862 65.3864 1.0000 9.2001 84.6421 1.0000 9.1224 83.2176 1.0000 10.1921 103.8794 1.0000 11.0629 122.3885 1.0000 13.8606 192.1154 1.0000 13.7241 188.3507 1.0000 15.8954 252.6645 in = 1.0e+03 * 0.0010 0.0002 0.0000 0.0000 0.0010 0.0007 0.0005 0.0003 0.0010 0.0033 0.0112 0.0375 0.0010 0.0043 0.0182 0.0777 0.0010 0.0047 0.0219 0.1025 0.0010 0.0058 0.0337 0.1957 0.0010 0.0060 0.0355 0.2119 0.0010 0.0081 0.0654 0.5287 0.0010 0.0092 0.0846 0.7787 0.0010 0.0091 0.0832 0.7591 0.0010 0.0102 0.1039 1.0588 0.0010 0.0111 0.1224 1.3540 0.0010 0.0139 0.1921 2.6628 0.0010 0.0137 0.1884 2.5849 0.0010 0.0159 0.2527 4.0162 ans = -10.1779 0.3816 -0.5301 0.4012

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 ans = 0.1230 0.4560

Suggested Problems

More from this Author7

Community Treasure Hunt

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

Start Hunting!