
maxdur = 0.0022; % sec
res = .1;	% mm resolution
FOV = 30;	% cm
dT = 0.000004;	% s
N=50;		% 		% Initial N

Ntoohigh = 300; 	% Don't go above
Ntoolow = 2;		% Don't go below
count=0;

while ((N>Ntoolow) && (N< Ntoohigh))
  count=count+1;

  g = whirl(N,res,FOV,dT);	% Initial G
  Nlast=N;

  t = [1:length(g)]*dT;
  plot(t*1000,real(g),'b--',t*1000,imag(g),'r--'); grid on;
  xlabel('Time (ms)'); ylabel('Gradients'); 
  axis([0 6 -4 4]); legend('G_x','G_y');
  tt = sprintf('WHIRL for %d Interleaves',N); title(tt); drawnow; setprops;
  fig2tiff('whirl',count);
 
  if (length(g)*dT < maxdur)
    Ntoohigh = N;
    N = round((N+Ntoolow)/2);	% Bisect interval
  else 
    Ntoolow = N;
    N = round((Ntoohigh+N)/2);	% Bisect interval
  end;

end;
N = Nlast;		% Probably good enough, could do N=Ntoohigh
			% if we like to be totally sure.




