finished regression chapter

This commit is contained in:
2015-11-08 18:10:06 +01:00
parent 520acdabcb
commit 4086da2f7a
8 changed files with 184 additions and 62 deletions

View File

@@ -0,0 +1,31 @@
clear
close all
load('lin_regression.mat');
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);
end
end
% plot the error surface
figure()
[N,M] = meshgrid(intercepts, slopes);
s = surface(M,N,error_surf);
xlabel('slope', 'rotation', 7.5)
ylabel('intercept', 'rotation', -22.5)
zlabel('error')
set(gca,'xtick', (-5:2.5:5))
grid on
view(3)
set(gcf, 'paperunits', 'centimeters', 'papersize', [15, 15], ...
'paperposition', [0., 0., 15, 15])
saveas(gcf, 'error_surface', 'pdf')

View File

@@ -1,4 +1,4 @@
function error = lsq_error(parameter, x, y)
function error = lsqError(parameter, x, y)
% Objective function for fitting a linear equation to data.
%
% Arguments: parameter, vector containing slope and intercept (1st and 2nd element)

View File

@@ -0,0 +1,7 @@
function gradient = lsqGradient(parameter, x, y)
h = 1e-6;
partial_m = (lsqError([parameter(1)+h, parameter(2)],x,y) - lsqError(parameter,x,y))/ h;
partial_n = (lsqError([parameter(1), parameter(2)+h],x,y) - lsqError(parameter,x,y))/ h;
gradient = [partial_m, partial_n];

View File

@@ -1,7 +0,0 @@
function gradient = lsq_gradient(parameter, x, y)
h = 1e-6;
partial_m = (lsq_error([parameter(1)+h, parameter(2)],x,y) - lsq_error(parameter,x,y))/ h;
partial_n = (lsq_error([parameter(1), parameter(2)+h],x,y) - lsq_error(parameter,x,y))/ h;
gradient = [partial_m, partial_n];