33 lines
715 B
Matlab
33 lines
715 B
Matlab
% x, y from exercise 8.3
|
|
|
|
% some arbitrary values for the slope and the intercept to start with:
|
|
position = [-2.0, 10.0];
|
|
|
|
% gradient descent:
|
|
gradient = [];
|
|
errors = [];
|
|
count = 1;
|
|
eps = 0.0001;
|
|
while isempty(gradient) || norm(gradient) > 0.1
|
|
gradient = meanSquaredGradient(x, y, position);
|
|
errors(count) = meanSquaredError(x, y, position);
|
|
position = position - eps .* gradient;
|
|
count = count + 1;
|
|
end
|
|
|
|
figure()
|
|
subplot(2,1,1)
|
|
hold on
|
|
scatter(x, y, 'displayname', 'data')
|
|
xx = min(x):0.01:max(x);
|
|
yy = position(1).*xx + position(2);
|
|
plot(xx, yy, 'displayname', 'fit')
|
|
xlabel('Input')
|
|
ylabel('Output')
|
|
grid on
|
|
legend show
|
|
subplot(2,1,2)
|
|
plot(errors)
|
|
xlabel('optimization steps')
|
|
ylabel('error')
|