function markov ( trial_num )

%*****************************************************************************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 probility P(I,J).
%    Simulate this process over many steps.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
%  Input:
%
%    integer trial_num: the number of trials.
%
  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;

  prob0 = zeros ( trial_num, 1 );
  prob1 = zeros ( trial_num, 1 );
  prob2 = zeros ( trial_num, 1 );
  prob3 = zeros ( trial_num, 1 );

  for n = 1 : trial_num

    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 : trial_num
  hold ( 'on' );
    plot ( n, prob3, '.' );
    plot ( n, prob2, '.' );
    plot ( n, prob1, '+' );
    plot ( n, prob0, '*' );
  hold ( 'off' );
  legend ( 'state 3', 'state 2', 'state 1', 'state 0' );
  xlabel ( 'time n (microseconds)' );
  ylabel ( 'probability' );
  title ( 'Probability the Path is in State k at Time n' );
  grid ( 'on' );
  filename = 'markov.png';
  print ( '-dpng', filename );
  fprintf ( '  Graphics saved as "%s"\n', filename );

  return
end
