Levenberg-Marquardt method for nonlinear elliptical equation
n=300;
r=linspace(-pi5,pi5,n)';
h=r(2)-r(1);
rb=[r(1)-h;r;r(end)+h];
f=-sin(r)+sin(r).^2;
x0=sin(rb);
u=cos(r).*sin(r); % initial guess
% ************ D2********************
cn1=-1ones(n-1,1);
cp1=1ones(n-1,1);
c=-2ones(n,1);
D2=sparse((diag(c,0)+diag(cp1,1)+diag(-cn1,-1))/(h^2));
%*****************************************
M=D2;
eta=0.9;
for k=1:200
lam=norm(F(u));
L=g(u)'g(u)+lamspeye(n,n);
R=-g(u)'F(u); %之前转置写掉了;
d=L\R;
if norm(F(u+d))<=norm(etaF(u))
u=u+d;
else
alp=Armijo(u,d);
u=u+alp*d;
end
plot(r,u,'b.',r,sin(r),'r')
title(['res=',num2str(nF(u)), ', k=',num2str(k),' , \alpha=',num2str(norm(d))])
drawnow
end
function y=F(u)
y=M*u+u.^2-f;
y(1)=y(1)+x0(1)/h^2;
y(end)=y(end)+x0(end)/h^2;
end
function y=g(u)
J=M+diag(2*u,0);
y=J;
end
function y=nF(u)
y=norm(F(u));
end
function alp=Armijo(u,d)
et=1/5;
bet1=0.5;
t=0;
while t<=500
if (nF(u+et^t*d)^2<=nF(u)^2+bet1*et^t*F(u)'*g(u)*d)
alp=et^t;
break
else
t=t+1;
end
alp=et^t;
end
end
end