28 lines
654 B
Matlab
28 lines
654 B
Matlab
% generate data:
|
|
m = 0.75;
|
|
b = -40.0;
|
|
n = 20;
|
|
x = 120.0*rand(n, 1);
|
|
y = m*x + b + 15.0*randn(n, 1);
|
|
|
|
% compute mean squared error for a range of slopes and intercepts:
|
|
slopes = -5:0.25:5;
|
|
intercepts = -30:1:30;
|
|
error_surface = zeros(length(slopes), length(intercepts));
|
|
for i = 1:length(slopes)
|
|
for j = 1:length(intercepts)
|
|
error_surf(i,j) = meanSquaredError(x, y, [slopes(i), intercepts(j)]);
|
|
end
|
|
end
|
|
|
|
% plot the error surface:
|
|
figure()
|
|
[N,M] = meshgrid(intercepts, slopes);
|
|
surface(M, N, error_surface);
|
|
xlabel('slope', 'rotation', 7.5)
|
|
ylabel('intercept', 'rotation', -22.5)
|
|
zlabel('error')
|
|
set(gca,'xtick', (-5:2.5:5))
|
|
grid on
|
|
view(3)
|