62 lines
949 B
Matlab
62 lines
949 B
Matlab
close all
|
|
clear
|
|
|
|
m = 50;
|
|
arange = linspace(0,1,m);
|
|
brange = linspace(.5,1.5, m);
|
|
|
|
[A,B] = meshgrid(arange, brange);
|
|
|
|
E = 0*A;
|
|
|
|
x = linspace(-5,5,20);
|
|
y = .5*x + 1 + randn(1, length(x));
|
|
|
|
U = 0*A;
|
|
V = 0*A;
|
|
|
|
|
|
for i = 1:m
|
|
for j = 1:m
|
|
E(i,j) = lserr([A(i,j), B(i,j)], x, y);
|
|
grad = lserr_gradient([A(i,j), B(i,j)], x, y);
|
|
U(i,j) = grad(1);
|
|
V(i,j) = grad(2);
|
|
|
|
end
|
|
end
|
|
colormap('gray');
|
|
|
|
subplot(1,2,1);
|
|
hold on
|
|
surf(A,B,E, 'FaceAlpha',.5);
|
|
shading interp
|
|
pause
|
|
subplot(1,2,2);
|
|
plot(x,y,'ok');
|
|
|
|
%%
|
|
|
|
t = linspace(-5,5,100);
|
|
param0 = [1,1];
|
|
step = 0.01;
|
|
param = param0;
|
|
|
|
|
|
for i = 1:100
|
|
err = lserr(param, x, y);
|
|
derr = lserr_gradient(param, x, y);
|
|
subplot(1,2,1);
|
|
plot3(param(1), param(2), err,'or');
|
|
|
|
subplot(1,2,2);
|
|
hold off
|
|
plot(x,y,'ok');
|
|
hold on
|
|
plot(t, param(1)*t + param(2), '--k', 'LineWidth',2);
|
|
pause(0.2);
|
|
param = param - step*derr;
|
|
|
|
end
|
|
|
|
hold off |