Make Random Walk into 3D

9 ビュー (過去 30 日間)
Kelly Vermilyea
Kelly Vermilyea 2019 年 9 月 18 日
編集済み: Bruno Luong 2020 年 8 月 27 日
I have a code set up for a randomwalk already, but how would I alter it to make a 3D Random Walk?
steplenght = .01;
positions = zeros(2,100000);
currentPosition = [0;0];
currentTimeStep = 2;
while currentPosition(1,1)^2 + currentPosition(2,1)^2 < 1
randomDirection = 2*pi*rand();
unitDisplacementVector = [cos(randomDirection); sin(randomDirection)];
displacementVector = stepLength*unitDisplacementVector;
currentPosition = currentPosition + displacementVector;
positions(:,currentTimeStep) = currentPosition;
currentTimeStep = currentTimeStep + 1;
end
positions = positions(:,1:currentTimeStep-1);
hold on;
scatter(positions(1,:),positions(2,:),2,'filled');
theta = linspace(0,2*pi);
plot(cos(theta),sin(theta));
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 9 月 18 日
It is confusing that you have
steplenght = .01;
but your code refers to stepLength . Someone reading the code would be wondering whether there was the intention that the two different variables are related somehow.

サインインしてコメントする。

採用された回答

darova
darova 2019 年 9 月 18 日
I do this
dt = .01;
[x, y, z] = deal( 0 );
[X,Y,Z] = sphere(30);
surf(X,Y,Z,'edgecolor',[1 1 1]*0.8,'Facecolor','none')
hold on
k = 0;
while norm([x y z])^2 < 1 && k < 1000
t = 180*(rand-1);
p = 180*(rand-1);
% spherical system of coordinates
x = x + dt*cosd(t)*cosd(p);
y = y + dt*cosd(t)*sind(p);
z = z + dt*sind(p);
k = k + 1;
pause(0.1)
plot3(x,y,z,'.b')
end
hold off
  2 件のコメント
ST
ST 2020 年 8 月 27 日
Can you update the code for the Random Waypoint Mobility in 3D? I want to add different velocities, directions and pause times for each node within the boundaries of 3D cube.
Bruno Luong
Bruno Luong 2020 年 8 月 27 日
編集済み: Bruno Luong 2020 年 8 月 27 日
The code by danova is flawed in the sense that create an anisotropic walk. The particle ends up systematcally near the pole. Generate direction with uniform spherical angle coordinates will condense the direction to north/south.
I call this climatic walk. ;-)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by