Fixed half of the regression chapter.

This commit is contained in:
2015-11-11 19:31:38 +01:00
parent ed361930fa
commit a0e4d306f1
10 changed files with 312 additions and 96 deletions

View File

@@ -3,16 +3,17 @@ close all
load('lin_regression.mat');
% compute mean squared error for a range of sloopes and intercepts:
slopes = -5:0.25:5;
intercepts = -30:1:30;
error_surf = zeros(length(slopes), length(intercepts));
for i = 1:length(slopes)
for j = 1:length(intercepts)
error_surf(i,j) = lsqError([slopes(i), intercepts(j)], x, y);
error_surf(i,j) = lsqError(x, y, [slopes(i), intercepts(j)]);
end
end
% plot the error surface
% plot the error surface:
figure()
[N,M] = meshgrid(intercepts, slopes);
s = surface(M,N,error_surf);

View File

@@ -1,11 +1,13 @@
function error = lsqError(parameter, x, y)
function error = lsqError(x, y, parameter)
% Objective function for fitting a linear equation to data.
%
% Arguments: parameter, vector containing slope and intercept (1st and 2nd element)
% x, the input values
% y, the measured system output
% Arguments: x, the input values
% y, the corresponding measured output values
% parameter, vector containing slope and intercept
% as the 1st and 2nd element
%
% Retruns: the estimation error in terms of the mean sqaure error
% Returns: the estimation error in terms of the mean sqaure error
y_est = x .* parameter(1) + parameter(2);
error = meanSquareError(y, y_est);
end

View File

@@ -1,9 +1,10 @@
function error = meanSquareError(y, y_est)
% Function calculates the mean sqauare error between observed and predicted values.
% Mean squared error between observed and predicted values.
%
% Agruments: y, the observed values
% y_est, the predicted values.
% Arguments: y, vector of observed values.
% y_est, vector of predicted values.
%
% Returns: the error in the mean-square-deviation sense.
% Returns: the error in the mean-squared-deviation sense.
error = mean((y - y_est).^2);
error = mean((y - y_est).^2);
end