Almost finished gradient descent

This commit is contained in:
2015-11-12 00:56:53 +01:00
parent a0e4d306f1
commit d3c8966358
7 changed files with 237 additions and 66 deletions

View File

@@ -1,6 +1,3 @@
clear
close all
load('lin_regression.mat');
% compute mean squared error for a range of sloopes and intercepts:
@@ -16,14 +13,10 @@ end
% plot the error surface:
figure()
[N,M] = meshgrid(intercepts, slopes);
s = surface(M,N,error_surf);
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,7 +1,17 @@
function gradient = lsqGradient(parameter, x, y)
h = 1e-6;
function gradient = lsqGradient(x, y, parameter)
% The gradient of the least square error
%
% Arguments: x, the input values
% y, the corresponding measured output values
% parameter, vector containing slope and intercept
% as the 1st and 2nd element
%
% Returns: the gradient as a vector with two elements
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;
h = 1e-6; % stepsize for derivatives
gradient = [partial_m, partial_n];
partial_m = (lsqError(x, y, [parameter(1)+h, parameter(2)]) - lsqError(x, y, parameter))/ h;
partial_n = (lsqError(x, y, [parameter(1), parameter(2)+h]) - lsqError(x, y, parameter))/ h;
gradient = [partial_m, partial_n];
end