26 lines
679 B
Matlab
26 lines
679 B
Matlab
% data:
|
|
load('lin_regression.mat')
|
|
|
|
% compute mean squared error for a range of slopes and intercepts:
|
|
slopes = -5:0.25:5;
|
|
intercepts = -30:1:30;
|
|
errors = zeros(length(slopes), length(intercepts));
|
|
for i = 1:length(slopes)
|
|
for j = 1:length(intercepts)
|
|
errors(i,j) = lsqError([slopes(i), intercepts(j)], x, y);
|
|
end
|
|
end
|
|
|
|
% minimum of error surface:
|
|
[me, mi] = min(errors(:));
|
|
[ia, ib] = ind2sub(size(errors), mi);
|
|
eparams = [errors(ia), errors(ib)];
|
|
|
|
% gradient descent:
|
|
pstart = [-2. 10.];
|
|
[params, errors] = descent(x, y, pstart);
|
|
|
|
% comparison:
|
|
fprintf('descent: %6.3f %6.3f\n', params(1), params(2));
|
|
fprintf('errors: %6.3f %6.3f\n', eparams(1), eparams(2));
|