function [w,err,its] = StatVec(P,v,tol,kMax)
% P is an n-by-n array of transition probabilities
% v is an n-by-1 state vector
% Iterates until there is the changes in the state vector
% are less than tol where tol is a small positive tolerance.
% A maximum of kMax iterations are performed.
% w is the approximate stationary vector and err is an estimate of
% the error in its components.

  w = Update(P,v);
  err = max(abs(w-v));
  k = 1;
  while k<kMax && err>tol
      v = w;
      w = Update(P,v);
      err = max(abs(w-v));
      k = k+1;
  end
  its = k;