Cody

Problem 258. linear least squares fitting

Solution 1683437

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)

Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 4-by-1. Error in fit_coefficients (line 4) a(:,c) = f{c}(x) 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)

Unable to perform assignment because the size of the left side is 2-by-1 and the size of the right side is 5-by-1. Error in fit_coefficients (line 4) a(:,c) = f{c}(x) 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)

Unable to perform assignment because the size of the left side is 4-by-1 and the size of the right side is 15-by-1. Error in fit_coefficients (line 4) a(:,c) = f{c}(x) 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)

Unable to perform assignment because the size of the left side is 2-by-1 and the size of the right side is 63-by-1. Error in fit_coefficients (line 4) a(:,c) = f{c}(x) Error in Test4 (line 6) assert(norm(fit_coefficients(f,x,y)-aref)<1e-6)