% This m-file computes the first passage time 
% probabilities  from state i to state j over n steps 
% to use specify i , j , n and P
% fij vector contains the n passage probabilities

fij(1)  = P(i,j);
for l=1:n
  T = P^l;
  pij(l) = T(i,j);
  pjj(l) = T(j,j);
end

for l =2:n 
sum = 0;
for r= 1:(l-1)
 sum = sum +  fij(r)*pjj(l-r);
end
fij(l) = pij(l) - sum;
end
fij

