function OUT = var_sim_robust(L,Z,d)

%VAR_SIM estimates a spherical variogram for a collection of
%k data sets using using VAR_ROBUST and VAR_SPHER.

%Written by: TONY E. SMITH, 2/21/98

%Functions called: variogram.m, var_fit.m, spher.m

%INPUTS:  L = (nx2)-matrix of locations (Xi,Yi), i = 1:n,
%         Z = (nxk)-matrix of k data sets (Zi) for locations in L.
%         d = bandwidth for estimation .
%
%OUT = (kx4)-matrix [ri,si,ai,vi] with (ri,si,ai) denoting the 
%      variogram parameter estimates, and vi the sample variance 
%      estimate for data set i.

[n,k] = size(Z);

OUT = zeros(k,3);

M(:,1:2) = L;

index = 10;

for i = 1:k
   
   M(:,3) = Z(:,i);
           
	DAT = var_robust(M,d) ;

	D = DAT(:,1) ;

	G = DAT(:,2) ;

	y = zeros(3,1) ;

	y(1) = max(D) ;      %initial estimate of range

	y(2) = max(G) ;        %initial estimate of sill

	y(3) = max([0,min(G)]) ; %initial estimate of nugget

	DAT = var_fit(y,D,G) ;

	OUT(i,:) = DAT{1}';
   
   %screen counter
   
   if 100*(i/k) >= index
      
      percent_done = index
      
      index = index + 10;
      
   end %end simulation loop 
      
end

%compute sample variance (with normalizer = n-1)
   
v = (std(Z).^2)';
   
OUT = [OUT,v];

