Plotting a random walk step by step
128 次查看(过去 30 天)
显示 更早的评论
Hello!
I'm trying to plot a random walk in 2D on a circle with the plot being updated every time the particle takes a step (not just when an entire particle walk iteration is complete). It has 2 particles with 20 steps each. It runs fine with the first particle, then the drawing lines become crossed. I know it has something to do with the drawing command, but I am new to Matlab and am clueless. This is not MY code, I am just trying to modify it for my own practice. Run it and you'll see what I mean. Any help will assist in my learning. Thank you!
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
clc;
clearvars;
stepsPerWalk = 20; % Length of the x-axis, also known as the length of the random walks.
numberOfWalks = 2; % The amount of random walks.
x_t(1) = 0;
y_t(1) = 0;
% Plot a circle
radius = 20;
pos = [-radius, -radius, 2*radius, 2*radius];
rectangle('Position',pos,'Curvature',[1 1], 'LineWidth', 3)
axis equal;
ax = gca;
ax.FontWeight = 'bold';
ax.FontSize = 20;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
hold on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
% Do the random walk.
for m = 1 : numberOfWalks
for n = 1 : stepsPerWalk % Looping all values of N into x_t(n).
distance = radius + 1;
while distance > radius
% Get new trial x_t and y_t
A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
if rand > 0.5
% Change only x.
x_t(n+1) = x_t(n) + A;
y_t(n+1) = y_t(n);
else
% Change only y.
x_t(n+1) = x_t(n);
y_t(n+1) = y_t(n) + A;
end
% Compute distance again with new values.
distance = sqrt(x_t(n+1)^2 + y_t(n+1)^2);
end
plot(x_t, y_t, 'LineWidth', 2);
caption = sprintf('Walk #%d of %d', m, numberOfWalks);
title(caption, 'FontSize', 25, 'FontWeight', 'bold');
hold on;
drawnow;
end
end
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
axis square;
回答(2 个)
Mathieu NOE
2021-1-13
hello
IMHO, the problem comes that x and y are not reinitialized (put to zero) when you start the next walk
so I changed this and now every new walk will start from the origin and previous walk values of x and y are flushed.
you could also change the code so that the second walk starts where the first stopped
hope it helped :
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
clc;
clearvars;
stepsPerWalk = 50; % Length of the x-axis, also known as the length of the random walks.
numberOfWalks = 5; % The amount of random walks.
% x_t(1) = 0;
% y_t(1) = 0;
% Plot a circle
radius = 20;
pos = [-radius, -radius, 2*radius, 2*radius];
rectangle('Position',pos,'Curvature',[1 1], 'LineWidth', 3)
axis equal;
ax = gca;
ax.FontWeight = 'bold';
ax.FontSize = 20;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
hold on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
% Do the random walk.
for m = 1 : numberOfWalks
x_t = 0; % reset x and y at start of iteration
y_t = 0;
for n = 1 : stepsPerWalk % Looping all values of N into x_t(n).
distance = radius + 1;
while distance > radius
% Get new trial x_t and y_t
A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
if rand > 0.5
% Change only x.
x_t(n+1) = x_t(n) + A;
y_t(n+1) = y_t(n);
else
% Change only y.
x_t(n+1) = x_t(n);
y_t(n+1) = y_t(n) + A;
end
% Compute distance again with new values.
distance = sqrt(x_t(n+1)^2 + y_t(n+1)^2);
end
plot(x_t, y_t, 'LineWidth', 2);
caption = sprintf('Walk #%d of %d', m, numberOfWalks);
title(caption, 'FontSize', 25, 'FontWeight', 'bold');
hold on;
drawnow;
end
end
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
axis square;
Steven Lord
2021-1-14
You're adding a new line to the axes each time you want to put a new point down. That's numberOfWalks times stepsPerWalk different lines. Instead, I recommend creating an animatedline and calling addpoints inside the loop to add a new point to the existing line.
x = 0:5:360;
h = animatedline('LineStyle', ':', 'Marker', 'o');
axis([0 360 -1 1])
for k = 1:numel(x)
addpoints(h, x(k), sind(x(k)));
drawnow expose
end
If you execute this in your MATLAB session you'll see the plot being constructed one point at a time. In Answers all you'll see is the final result.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Axes Appearance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!