function random_walk_2d_plot ( step_num, walk_num, filename ) %*****************************************************************************80 % %% RANDOM_WALK_2D_PLOT plots 1 or more random walks in 2D. % % Licensing: % % This code is distributed under the GNU LGPL license. % % Modified: % % 05 November 2009 % % Author: % % John Burkardt % % Parameters: % % Input, integer STEP_NUM, the number of steps to take in one test. % % Input, integer WALK_NUM, the number of walks to take. % % Input, string FILENAME, the name of the file in which to store % a copy of the image. % % % Clear the plotting frame. % clf hold on plot ( 0.0, 0.0, 'ko', 'MarkerSize', 10 ) grid on for walk = 1 : walk_num x = zeros(step_num+1,1); y = zeros(step_num+1,1); % % Take a walk of STEP_NUM random steps. % for step = 2 : step_num + 1 % % We are currently at ( X(STEP-1), Y(STEP-1) ). % Consider the four possible points to step to. % destination = [ x(step-1) + 1.0, y(step-1); ... x(step-1) - 1.0, y(step-1); ... x(step-1), y(step-1) + 1.0; ... x(step-1), y(step-1) - 1.0 ]; % % Choose destination 1, 2, 3 or 4. % k = ceil ( 4.0 * rand ); % % Move there. % x(step) = destination(k,1); y(step) = destination(k,2); plot ( [ x(step-1), x(step) ], [ y(step-1), y(step) ], 'r-', 'LineWidth', 1 ); end plot ( x(step_num+1), y(step_num+1), 'r*', 'MarkerSize', 10 ) % % Plot the results. % title_string = sprintf ( '2D Random Walk - %d walks, %d steps', ... walk, step_num ); title ( title_string ); xlabel ( 'X' ) ylabel ( 'Y' ); pause ( 5 ) for step = 2 : step_num plot ( [ x(step-1), x(step) ], [ y(step-1), y(step) ], 'b-', 'LineWidth', 1 ); end plot ( x(step_num+1), y(step_num+1), 'k*', 'MarkerSize', 10 ) end hold off % % Save the graphics file. % print ( '-dpng', filename ); fprintf ( 1, '\n' ); fprintf ( 1, ' Saving graphics file in "%s"\n', filename ); return end