This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/regression/exercises/linefit.m

19 lines
455 B
Matlab

% data:
load('lin_regression.mat')
% gradient descent:
pstart = [-2. 10.];
[params, errors] = descent(x, y, pstart);
% lsqcurvefit:
line = @(p, x) x.* p(1) + p(2);
cparams = lsqcurvefit(line, pstart, x, y);
% polyfit:
pparams = polyfit(x, y, 1);
% comparison:
fprintf('descent: %6.3f %6.3f\n', params(1), params(2));
fprintf('lsqcurvefit: %6.3f %6.3f\n', cparams(1), cparams(2));
fprintf('polyfit: %6.3f %6.3f\n', pparams(1), pparams(2));