0% found this document useful (0 votes)
67 views7 pages

LMSP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views7 pages

LMSP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

%lmsp

% [pstar,iter]=lmsp(func,jac,x0,tol,maxiter)
%
% Use the Levenberg-Marquardt algorithm to minimize
%
% f(x)=sum(F_i(x)^2)
%
% func name of the function F(x)
% jac name of the Jacobian function J(x)
% x0 initial guess
% tol stopping tolerance
% maxiter maximum number of iterations allowed
%
% Returns
% pstar best solution found.
% iter Iteration count.
%
function [pstar,iter]=lmsp(func,jacsp,p0,tol,maxiter)
%
% Initialize p and oldp.
%
p0=[1.0; 0.5; 0.60; -300.0]; % initial guess for depth, shape, degree and dipole moment
p=p0;
fp=norm(feval(func,p),2)^2;
oldp=p0*2;
oldfp=fp*2;
%
% Initialize lambda.
%
lambda=0.0001;
%
% The main loop. While the current solution isn't good enough, keep
% trying... Stop after maxiter iterations in the worst case.
%
iter=0;
while (iter <= maxiter)
%
% Compute the Jacobian.
%

1
J=feval(jacsp,p);
%
% Compute rhs=-J'*f
%
% rhs=-J'*feval(func,p);
%
% We use a clever trick here. The least squares problem
%
% min || [ J ] s - [ -F ] ||
% || [ sqrt(lambda)*I ] [ 0 ] ||
%
% Has the normal equations solution
%
% s=-inv(J'*J+lambda*I)*J'*F
%
% which is precisely the LM step. We can solve this least squares problem
% more accurately using the QR factorization then by computing
% inv(J'*J+lambda*I) explicitly.
%
rhs=-J'*feval(func,p);
myrhs=[-feval(func,p); zeros(length(p),1)];
s=[J; sqrt(lambda)*eye(length(p))]\myrhs;
%
% Check the termination criteria.
%
if ((norm(rhs,2)< sqrt(tol)*(1+abs(fp))) & ...
(abs(oldfp-fp)<tol*(1+abs(fp))) & ...
(norm(oldp-p,2)<sqrt(tol)*(1+norm(p,2))))
pstar=p;
return;
end
%
% See whether this improves chisq or not.
%
fpnew=norm(feval(func,p+s),2)^2;
%
% If this improves f, then make the step, and decrease lambda and make
% the step.
%
if (fpnew < fp)

2
oldp=p;
oldfp=fp;
p=p+s;
fp=fpnew;
lambda=lambda/2;
if (lambda <10^(-12))
lambda=1.0e-12;
end
iter=iter+1;
else
%
% Didn't improve f, increase lambda, and try again.
%
lambda=lambda*2.5;
if (lambda >10^16)
lambda=10^16;
end
iter=iter+1;
end
%
% end of the loop.
%
end
%
% Return, max iters exceeded.
%
pstar=p;
----------------------------------------------------------------------------
% p=chi2cdf(x,m)
%
% Computes the Chi^2 CDF, using a transformation to N(0,1)
% Note that x and m must be scalars.
%
function p=chi2cdf(x,m)
if (x == (m-1))
p=0.5;
else
z=(x-m+2/3-0.08/m)*sqrt((m-1)*log((m-1)/x)+x-(m-1) )/abs(x-m+1);
p=phi(z);
end

3
function J=jacsp(p)
%
% global variables.
global SIGMA;
global V;
global x;
%
n=length(x);
J=zeros(n,4);
for i=1:n
J(i,1)=(p(4)*sin(p(3))/((x(i)^2+p(1)^2)^p(2))-
2*p(4)*(x(i)*cos(p(3))+p(1)*sin(p(3)))/((x(i)^2+p(1)^2)^p(2))*p(2)*p(1)/(x(i)^2+p(1)^2))/SIGMA(i);
J(i,2)=(-p(4)*(x(i)*cos(p(3))+p(1)*sin(p(3)))/((x(i)^2+p(1)^2)^p(2))*log(x(i)^2+p(1)^2))/SIGMA(i);
J(i,3)=(p(4)*(-x(i)*sin(p(3))+p(1)*cos(p(3)))/((x(i)^2+p(1)^2)^p(2)))/SIGMA(i);
J(i,4)=((x(i)*cos(p(3))+p(1)*sin(p(3)))/((x(i)^2+p(1)^2)^p(2)))/SIGMA(i);
end

% Computes the differences between forward model prediction and data,


% normalized by the standard deviation for the slug test example.
function fvec=funsp(p)
%
% global variables.
global SIGMA;
global V;
global x;
%global Vnol;
%global xnol;
%global K;
%global teta;
%global Vmax;
%global Vmin;
%
% Compute the function values.
%
fvec=zeros(length(x),1);
for i=1:length(x)
fvec(i)=(p(4)*((x(i)*cos(p(3))+p(1)*sin(p(3)))/(((x(i))^2+p(1)^2)^p(2))) - V(i))/SIGMA(i);
end

4
%set figure fontsizes
function void=bookfonts
set(gca,'FontSize',18,'LineWidth',1.0);

%
% Calculates the normal distribution.
%
% z=phi(x)
%
% gives z=int((1/sqrt(2*pi))*exp(-t^2/2),t=-infinity..x)
%
%%%%%%%%%%%%% Forget the previous lines, now the real data!!!!

function z=phi(x)
if (x>=0);
z=.5+.5*erf(x/sqrt(2));
else
z=1-phi(-x);
end

% Nama file :LMinv


% Reset to a known state.
%
clear
rand('state',0);
randn('state',0);
%
% Global variables for observed V (SP anomaly) and x (measurement points)
global V;
global x;
global SIGMA;
%
% The unknown/estimated parameters are z=p(1), q=p(2), tetaak=p(3) and Kak=p(4):
% z is the depth of burial center and q is the shape factor
%
% The data. V is measured to the nearest mV
%%load V.dat;
%%load x.dat;
%load data2c.dat %%%%%%%%%%%%% Forget the previous lines, now the real data!!!!

5
%x=data2c(:,1);
%V=data2c(:,2);
load d1.dat %%%%%%%%%%%%% Forget the previous lines, now the real data!!!!
x=d1(:,1);
V=d1(:,2);
%%c% Fixed parameter values.
Data=load('d1.dat');
%
% Fixed parameter values
%
%
%sigma= 1mV (assumption); standard deviation is 1mV for all data
for i=1:length(x)
SIGMA(i)=1;
end
% Solve the least squares problem with LM.
%
%p0=[0.001; 1.0];
tic
p0=[1.0; 0.5; 0.60; -300.0]; % initial guess for depth, shape, degree and dipole moment
[pest,itercnt]=lmsp('funsp','jacsp',p0,1.0e-12,100); %calling lmsp.m, executing funsp.m and jacsp.m
%pest=estimated parameter; itercnt=number of iteration
%
toc
time = toc;
%% Inversi sebetulnya telah selesai

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Check the chi^2 value.
%
chi2=norm(funsp(pest))^2
pvalue=1-chi2cdf(chi2,length(V)-2)
%
% Compute the covariance matrix, correlation matrix, and confidence intervals.
%
Jest=jacsp(pest); % Jacobian of the estimated parameter

C=inv(Jest'*Jest);
m=max(size(C));
for i=1:m

6
for j=1:m
Corm(i,j)=C(i,j)/(sqrt(C(i,i))*sqrt(C(j,j)));
end
end
fprintf('Iteration number = %.i\n',itercnt);
fprintf('z=%.5f +- %.5f\n',[pest(1) 1.96*sqrt(C(1,1))]);
fprintf('q=%.5f +- %.5f\n',[pest(2) 1.96*sqrt(C(2,2))]);
fprintf('teta=%.5f +- %.5f\n',[pest(3)*(180/pi) 1.96*sqrt(C(3,3))*(180/pi)]);
fprintf('K=%.5f +- %.5f\n',[pest(4) 1.96*sqrt(C(4,4))]);
fprintf('Covariance matrix is \n');
C
fprintf('Correlation matrix is \n');
Corm
%
% Now, plot the data and fitted curve.
%
figure(1);
clf;
bookfonts;
%tfit=0.001:1.0:51;
xfit=x;
for i=1:length(xfit)
Vfit(i)=pest(4)*((xfit(i)*cos(pest(3))+pest(1)*sin(pest(3)))/(((xfit(i))^2+pest(1)^2)^pest(2)));
end
plot(xfit,Vfit,'r-');

hold on
errorbar(x,V,SIGMA,'ko');
xlabel('distance (m)');
ylabel('SP anomaly (mV)');
legend 'calculated data'
grid on

%save padamelan2c.mat
save hasild1xxx.mat

You might also like