28 lines
679 B
Matlab
28 lines
679 B
Matlab
% generate data:
|
|
m = 3.0;
|
|
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 = 0.0:0.1:5.0;
|
|
intercepts = -200:10:200;
|
|
error_surface = zeros(length(intercepts), length(slopes));
|
|
for i = 1:length(intercepts)
|
|
for j = 1:length(slopes)
|
|
error_surface(i,j) = meanSquaredError(x, y, [slopes(j), intercepts(i)]);
|
|
end
|
|
end
|
|
|
|
% plot the error surface:
|
|
figure()
|
|
[ss, ii] = meshgrid(slopes, intercepts);
|
|
surface(ss, ii, error_surface);
|
|
xlabel('slope', 'rotation', 7.5)
|
|
ylabel('intercept', 'rotation', -22.5)
|
|
zlabel('mean squared error')
|
|
set(gca,'xtick', (-5:2.5:5))
|
|
grid on
|
|
view(3)
|