function markov ( )

%*****************************************************************************80
%
%% MARKOV follows a Markov chain process.
%
%  Discussion:
%
%    We have four states, 1, 2, 3, 4.
%    If we are in state I, then on the next step we will move to state J
%    with a proability P(I,J).
%    Simulate this process over many steps.
%
%  Modified:
%
%    29 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'MARKOV\n' );
  fprintf ( 1, '  We have four states, 1, 2, 3, 4.\n' );
  fprintf ( 1, '  If we are in state I, then on the next step we will move to state J\n' );
  fprintf ( 1, '  with a proability P(I,J).\n' );
  fprintf ( 1, '  Simulate this process over many steps.\n' );
  fprintf ( 1, '  Display the results graphically.\n' );

  p0 = [1 0 0 0];

  r1 = [.97 .03 0 0];
  r2 = [0 .98 .02 0];
  r3 = [0 0 .99 .01];
  r4 = [0 0 0 1];

  A = [r1;r2;r3;r4];
  B = A;

  for n = 1 : 800

    p = p0 * B;
    C = A * B;
    B = C;

    prob3(n) = p(4);
    prob2(n) = p(3);
    prob1(n) = p(2);
    prob0(n) = p(1);

  end
%
%  Graphics.
%
  clf
  n = 1 : 800;
  plot(n,prob3,n,prob2,'.',n,prob1,'+',n,prob0,'*')
  legend('state 3','state 2','state 1','state 0')
  xlabel('time n (in microseconds)')
  ylabel('probability')
  title('Fig.3.5-Probability the Path is in State k at Time n')
  grid
  print ( '-dpng', 'markov.png' )

  return
end
