axes(ax1)
zpch=ztop-4*rb;
for i=1:Xl
  bposcol(i)=plot(Xini(i),ztop-rb,'.','markersize',mksz,'color',col(i,:));
end

ax2=axes('position',[0.84 0.08 0.14 0.88],'color',[1 1 1]); box on, hold on
set(ax2,'xtick',[],'ytick',[])
plot([0 0],t([1 end]),'k:')
plot(Aforc,t,'b-')
axis tight
xlabel('Forcing'), ylabel('Time')

axes(ax1)

n2=1;
for n=1:nt
  A=interp1(xfull,a,X);
  A2=A-drag*U;
  AFM=interp1(xfull,afm,X);
  A2=A2+Aforc(n)*AFM;
  U=U+A2/2*dt;
  X=X+U*dt;
  RBV=interp1(xfull,rbv,X);
  Z=interp1(xfull,zfull,X)+RBV;
  EQ(:)=1; EQ(X>xeq2)=2;
  if ~mod(n-1,dts) & 0
    n2=n2+1;
    Aarr(:,n2)=A;
    Uarr(:,n2)=U;
    Xarr(:,n2)=X;
    EQarr(:,n2)=X;
  end

  axes(ax1)
  for i=1:Xl
    try, delete(bpos(i)), end
    try, delete(bpospos(i)), end
    bpos(i)=plot(X(i),Z(i),'.','markersize',mksz,'color',col(i,:));
    bpospos(i)=plot(Xini(i),zpch,'.','markersize',mksz,'color',coleq(EQ(i),:));
  end
  if 1
    axes(ax2)
    try, delete(bforc), end
    bforc=plot([0 Aforc(n)],[t(n) t(n)],'b-','linewidth',2);
  end
  if n==1
    if isempty(pstm1), disp('Press "s" to start'), pause, else, pause(pstm1), end
  else
    pause(pstm)
  end
  
  U=U+A2/2*dt;
  X=X+U*dt;  
end