www.pudn.com > LM_opti.rar > LM_opti.m
% Levenberg-Marquardt directions using BOX function
clc
clear
% Starting point
p0 = 5; q0 = 1;
x=[0.1; 0.2; 0.3; 0.4; 0.5; 0.6; 0.7; 0.8; 0.9; 1.0];
% target values
t = exp(-x) - exp(-10.0*x);
point_n = 21;
p = linspace(-1, 6, point_n);
q = linspace(0, 11, point_n);
[pp, qq] = meshgrid(p, q);
E = zeros(point_n, point_n);
for i = 1:point_n,
for j = 1:point_n,
y = exp(-pp(i,j)*x)-exp(-qq(i,j)*x);
E(i,j) = (t-y)'*(t-y);
end
end
% subplot(2,2,1);
figure
h = mesh(pp, qq, E);
set(h, 'facecolor', 'none');
xlabel('p'); ylabel('q'); zlabel('E(p,q)');
axis([-inf inf -inf inf -inf inf]);
set(gca, 'box', 'on');
y = exp(-x)-exp(-10*x);
E_tmp = (t-y)'*(t-y) ;
line(1, 10, E_tmp, 'linestyle', '*');
hold on
y = exp(-p0*x)-exp(-q0*x);
E_tmp = (t-y)'*(t-y) ;
line(p0, q0, E_tmp, 'linestyle', 'o');
% plot(p0, q0, 'x');
hold off
view([-230 30]);
figure
% contour(pp, qq, E, 500);
contour(pp, qq, E, 30);
line(1, 10, 'linestyle', '*');
hold on
plot(p0, q0, 'o');
hold on
theta(:,1)=[p0;q0];thetaold=[0;0];k=1;
point_n = 10;lambda=0.07;
while sum(sqrt(abs(theta(:,k)-thetaold)))>0.01
thetaold=theta(:,k);
k=k+1;
J = zeros(point_n, 2);
lm_pos=zeros(10, 2);
J = [-(-x.*exp(-thetaold(1)*x)) -(x.*exp(-thetaold(2)*x)) ];
y = exp(-thetaold(1)*x)-exp(-thetaold(2)*x);
g = 2.0 * J' * ( t - y );
JJ = J'*J;
lm_dir = -inv(JJ +lambda*eye(size(JJ)))*g;
if k==2
p_cur = [ p0; q0 ];
% For displaying purpose, arrow length is set to 2.
lm_dirtmp = 2.0 * lm_dir / norm(lm_dir);
lm_pos = [p_cur p_cur+lm_dirtmp];
% arrow(lm_pos(1, :), lm_pos(2, :), 0.2, 'r-');
text(2.8, 2.9, 'LM','fontsize', 10);
end
theta(:,k)=thetaold+lm_dir;
end
% plot(theta(1,1:k),theta(2,1:k),'o');
% hold on
plot(theta(1,1:k),theta(2,1:k))
xlabel('p'); ylabel('q');
axis image
hold off