How do I make a 2D randomwalk?
显示 更早的评论
I have so far only been able to make a 1D randomwalk but I have to make it into 2D. Below is my code for 1D. How do I change it so that it is in 2D?
clear
clc
N = 100; % Length of the x-axis, also known as the length of the random walks.
M = 400; % The amount of random walks.
x_t(1) = 0;
for m=1:M
for n = 1:N % Looping all values of N into x_t(n).
A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
x_t(n+1) = x_t(n) + A;
end
plot(x_t);
hold on
end
采纳的回答
Image Analyst
2018-2-17
编辑:Image Analyst
2018-2-17
Just duplicate everything for y:
clc;
clearvars;
N = 100; % Length of the x-axis, also known as the length of the random walks.
M = 400; % The amount of random walks.
x_t(1) = 0;
y_t(1) = 0;
for m=1:M
for n = 1:N % Looping all values of N into x_t(n).
A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
x_t(n+1) = x_t(n) + A;
A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
y_t(n+1) = y_t(n) + A;
end
plot(x_t, y_t);
hold on
end
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
axis square;

For what it's worth, see my attached random walk demos.
13 个评论
Mine looks like the one I just attached, sort of going sideways.
Image Analyst
2018-2-17
编辑:Image Analyst
2018-2-17
I can't see the one you attached. Attach a .PNG screenshot instead. The code I gave you does produce an image like in the link you shared.

Why do you think it doesn't?
Hmm... Maybe I'm thinking wrongly but it looks like it goes sideways instead of "up", "down" etc.
Image Analyst
2018-2-17
编辑:Image Analyst
2018-2-17
If you limit the steps to EITHER the x direction of the y direction, but NOT BOTH, then the grid would be sideways. However, if you require that BOTH x and y change by 1, then it will look like it does. If you don't want that, then just change ONE of them, not both simultaneously. See the code below:
clc;
clearvars;
N = 100; % Length of the x-axis, also known as the length of the random walks.
M = 400; % The amount of random walks.
x_t(1) = 0;
y_t(1) = 0;
for m=1:M
for n = 1:N % Looping all values of N into x_t(n).
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
end
plot(x_t, y_t, 'LineWidth', 2);
hold on
end
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
axis square;

Alright.
How do plot the position of all the particles? I tried using 'Markersize' but it doesn't seem to make them larger.
I created a circle using linspace. How can I create code that makes all randomwalks stay within this circle? Assuming they reach the circles surface they, with the help of IF, changes to the opposite direction instead of going through the circle.
I tried making IF statement in the For-loop but they keep going through it.
Not hard. See if you can do it yourself. Just get the distance of the current point from the origin and keep trying while the distance > radius.
distance = sqrt(x_t(n+1)^2 + y_t(n+1)^2);
while distance > radius
% Get new trial x_t and y_t
x_t(n+1) = .......... etc.
% Compute distance again with new values.
distance = sqrt(x_t(n+1)^2 + y_t(n+1)^2);
end
Anyway, you're a smart engineer so I'm sure you can figure it out yourself without me telling you.
Alright, seems like you're having trouble, so here is the complete code:
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 = 100; % Length of the x-axis, also known as the length of the random walks.
numberOfWalks = 400; % 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).
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
distance = sqrt(x_t(n+1)^2 + y_t(n+1)^2);
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
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
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
axis square;

How can I plot the number of random walks and then see the steps they ALL take simultaneously? As it is now, my script plots the movement of one particle to N steps, then it takes another particle and continues this way M times. How can I immediately start with M particles active and see them take N steps instead of one by one?
How would you do a 3D random walk with the code above?
Just add a z variable and use plot3().
Hi, how can i calculate the distance from the origin to all points taken in every step iteration? Thanks a lot
@Andy Paulo Ureña make a new array called allDistances, and assign it distances
allDistances(n) = distance;
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
