function [Xnew,spare]=LM(X,F,G,Ini,spare); % This function implements a Levenberg Marquadt Increment, according to % the algorithm given in 'Practical Methods of Optimization', Vol I, by % R.Fletcher, pp.84 % % In the first call, Ini should be set equal to 1, % 0 afterwards % % Input Arguments: % X - design variables % F - the error vector obtained using the variables X % G - The Jacobian matrix of the problem at point X % spare - a vector containing previous values; its format is % known by the functions WrapSpare and UnWrapSpare % % Output Arguments % Xnew - the new design variables % spare % % Written on 16, June, 1990 by Antonio Ruano [m,nvars]=size(G); [k,l]=size(X); if k==1 row=1; else row=0; end; X=X(:); F=F(:); if Ini~=1 % not initialization [Xold,Fold,Gold,Eold,Fest,lambda]=UnWrapSp(spare,m,nvars); ESN=F'*F; ESO=Fold'*Fold; ESE=Fest'*Fest; DeltaF=ESO-ESN; DeltaQ=ESO-ESE; r=DeltaF/DeltaQ; if r<.25 lambda=lambda*4; elseif r>.75 lambda=lambda/2; end; if r>0 % the test point is accepted Xold=X; Gold=G; Fold=F; Eold=G'*F; end; else % It's initialization lambda=1; Xold=X; Gold=G; Fold=F; Eold=G'*F; end; % Compute new point and the estimated sum of squares sd =-(Gold'*Gold+lambda*eye(nvars))\Eold; Xnew=Xold+sd; if row==1 Xnew=Xnew'; end; Fest=Gold*sd+Fold; spare=WrapSpar(Xold,Fold,Gold,Eold,Fest,lambda);