function results = var_fit_zeroed(y,D,G)

%VAR_FIT = least-squares fit of spherical variogram with zero nugget

%Written by: TONY E. SMITH, 2/20/98
%Modified with Truncation at max(D)/2, 2/23/04
%
%Functions called: err_fit_zeroed.m
%
%INPUTS:
%
%  (i)   y = [ r0 s0 ] = initial values of [r s]
%  (ii)  D = vector of distance values
%  (iii) G = vector of empirical variogram values
%  
%OUTPUTS: results.param = [r,s,a] = estimated range, sill, and 
%                  nugget values of the spherical variogram.
%         results.iter  = number of iterations (max = 600)

u = ones(length(D),1) ;

% [p,opt] = fminsearch('err_fit',y,[],u,D,G);
% 
% p = abs(p) ;

timeo = clock;

[p,err,exitflag,output] = fminsearch('err_fit_zeroed',y,[],u,D,G);

time_fminsearch = etime(clock,timeo); %check if too long

if exitflag == 0 
    
	fprintf(1,'VAR_FIT: convergence not obtained in %3d iterations \n',output.iterations);
	
	y0 = y(2);
	
	[p0,err,exitflag,output] = fminsearch('err_fit_truncated_zeroed',y0,[],u,D,G); %re-solves with range = max(D)
	
	p = [max(D),p0,0];
	
	disp(' ');
	disp('*******************************************************');
	disp(' ');
	disp('New estimation done with RANGE truncated to BANDWIDTH');
	disp(' ');
	disp('(Depending on data, you may wish to increase bandwidth)');
	disp(' ');
	disp('*******************************************************');

end


results.param = abs(p) ;

results.iter = output.iterations; %Number of iterations used in FMINS
