This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/regression/code/gradientDescent.m
2015-11-08 18:11:50 +01:00

31 lines
627 B
Matlab

clear
close all
load('lin_regression.mat')
position = [-2. 10.];
gradient = [];
errors = [];
count = 1;
eps = 0.01;
while isempty(gradient) || norm(gradient) > 0.1
gradient = lsqGradient(position, x,y);
errors(count) = lsqError(position, x, y);
position = position - eps .* gradient;
count = count + 1;
end
figure()
subplot(2,1,1)
hold on
scatter(x,y, 'displayname', 'data')
xaxis = min(x):0.01:max(x);
f_x = position(1).*xaxis + position(2);
plot(xaxis, f_x, 'displayname', 'fit')
xlabel('Input')
ylabel('Output')
grid on
legend show
subplot(2,1,2)
plot(errors)
xlabel('optimization steps')
ylabel('error')