Fixed half of the regression chapter.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user