% The following code is used to calculate the analytical optimal step
% length with the formula: (aT,res)/(aT,a)
% s: the reflectivity model for the current step
% D: real data


% Calculate the residual for current reflectivity model
res=forw(s)-D; 

% Migrate the residual to obtain the reflectivity correction
ds=mig(res,nx,nz,nt,dx,dz,dt,c,sx,W,nd2,TGX);

% Calculate the forward modeled data with the reflectivity correction 
a=forw(ds,nx,nz,nt,dx,dz,dt,c,sx,W,nd2,TGX);

% Calculate the analytical step length
a_res=dot(reshape(a,nt*nx,1),reshape(res,nt*nx,1));
a_a=dot(reshape(a,nt*nx,1),reshape(a,nt*nx,1));
alpha=a_res/a_a;