% Script Eg5_3
% Animates a 2-dimesnional random walk.

% Determine the grid size and generate the walk.
N = input('Enter N:');
[x y] = RandomWalk2D(N);

% Create the figure window
close all
figure
set(gcf,'position',[150 50 600 600])
hold on

% Draw the boundary and highlight the origin...
M = N+.5;
m = N-.5;
plot([-M M M -M -M],[-M -M M M -M],'k')
plot([-m m m -m -m],[-m -m m m -m],'r','linewidth',3)
fill([-.5 .5 .5 -.5 -.5],[-.5 -.5 .5 .5 -.5],'r')

% Set the axes
axis([-M M -M M])
axis equal square manual

% Place ticks at the half-integers...
set(gca,'xTick',linspace(-N-.5,N+.5,2*N+2),'yTick',linspace(-N-.5,N+.5,2*N+2))
set(gca,'xTickLabel',[])
set(gca,'yTickLabel',[])

% Turn the grid on
grid on

% Animates the walk...
for k=2:length(x)
plot(x(k-1),y(k-1),'.k','Markersize',20)
plot(x(k),y(k),'.r','Markersize',20)
title(sprintf('N = %1d Hops = %4d',N,k),'FontWeight','bold','FontSize',14)
pause(.02)
end