Cody

# Problem 258. linear least squares fitting

Solution 518973

Submitted on 31 Oct 2014 by Binbin Qi
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
%%% first test: fit to a constant 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 href = "matlab: helpview([docroot '/toolbox/optim/msg_csh/optim_msg_csh.map'],'local_min_lsq','CSHelpWindow');">Local minimum found</a>. Optimization completed because the <a href = "matlab: helpview([docroot '/toolbox/optim/msg_csh/optim_msg_csh.map'],'lsq_grad_size','CSHelpWindow');">size of the gradient</a> is less than the default value of the <a href = "matlab: helpview([docroot '/toolbox/optim/msg_csh/optim_msg_csh.map'],'function_tolerance_lsq','CSHelpWindow');">function tolerance</a>. <<a href = "matlab: createExitMsg('snls',1.000000e+00,true,true,'lsqcurvefit',1.110223e-16,'default',1.000000e-06,0.000000e+00,'',0.000000e+00,0.000000e+00,'',0.000000e+00);">stopping criteria details</a>>

2   Pass
%%% second test: fit to a straight line (linear regression) 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 href = "matlab: helpview([docroot '/toolbox/optim/msg_csh/optim_msg_csh.map'],'local_min_lsq','CSHelpWindow');">Local minimum found</a>. Optimization completed because the <a href = "matlab: helpview([docroot '/toolbox/optim/msg_csh/optim_msg_csh.map'],'lsq_grad_size','CSHelpWindow');">size of the gradient</a> is less than the default value of the <a href = "matlab: helpview([docroot '/toolbox/optim/msg_csh/optim_msg_csh.map'],'function_tolerance_lsq','CSHelpWindow');">function tolerance</a>. <<a href = "matlab: createExitMsg('snls',1.000000e+00,true,true,'lsqcurvefit',5.140705e-08,'default',1.000000e-06,0.000000e+00,'',0.000000e+00,0.000000e+00,'',0.000000e+00);">stopping criteria details</a>>

3   Pass
%%% third test: polynomial fit 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 href = "matlab: helpview([docroot '/toolbox/optim/msg_csh/optim_msg_csh.map'],'local_min_possible_lsq','CSHelpWindow');">Local minimum possible</a>. lsqcurvefit stopped because the <a href = "matlab: helpview([docroot '/toolbox/optim/msg_csh/optim_msg_csh.map'],'norm_current_step','CSHelpWindow');">size of the current step</a> is less than the default value of the <a href = "matlab: helpview([docroot '/toolbox/optim/msg_csh/optim_msg_csh.map'],'step_size_tol','CSHelpWindow');">step size tolerance</a>. <<a href = "matlab: createExitMsg('snls',2.000000e+00,true,true,'lsqcurvefit',2.760312e-07,'default',1.000000e-06,0.000000e+00,'',0.000000e+00,0.000000e+00,'',0.000000e+00);">stopping criteria details</a>>

4   Pass
%%% fourth test: non-polynomial fit (yes, we are that crazy) 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 href = "matlab: helpview([docroot '/toolbox/optim/msg_csh/optim_msg_csh.map'],'local_min_lsq','CSHelpWindow');">Local minimum found</a>. Optimization completed because the <a href = "matlab: helpview([docroot '/toolbox/optim/msg_csh/optim_msg_csh.map'],'lsq_grad_size','CSHelpWindow');">size of the gradient</a> is less than the default value of the <a href = "matlab: helpview([docroot '/toolbox/optim/msg_csh/optim_msg_csh.map'],'function_tolerance_lsq','CSHelpWindow');">function tolerance</a>. <<a href = "matlab: createExitMsg('snls',1.000000e+00,true,true,'lsqcurvefit',2.249518e-08,'default',1.000000e-06,0.000000e+00,'',0.000000e+00,0.000000e+00,'',0.000000e+00);">stopping criteria details</a>>

### Community Treasure Hunt

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

Start Hunting!