Why does my 3d randomwalk trend to the pole?

6 Ansichten (letzte 30 Tage)
Filip Berntsson
Filip Berntsson am 3 Mär. 2019
Kommentiert: Filip Berntsson am 4 Mär. 2019
Greetings!
I'm a student and I'm trying to put together a 3d random walk with a spherical constraint but my points tend to trend towards the north pole, (where Z goes to constraint Rk). I have the function tredslump (3dchance in swedish) and a script that calls the function and plots the points. Se below.
I have checked the array with coordinates and seen that the Z-dimension (pBook(:,3,i)) contains negative values and since the partilcles start at origin that means that movements in the negative Z-direction can happen.
What I'm not certain of is if i generated the random polar coordinates correctly or if the conversion to cartesian coordinates
I'd be most thankful for any input.
The following pictures are from the same run only at different tval() (see code). The first image only shows circles because the bulk of the particles are at the same point (origin).
t1.jpg
tval2.jpg
tval5.jpg
tval6.jpg
This is all the code that I'm using for this and it's runable:
The Script
Np=400; %number of particles
Rk=3; %radius on sphere
tmax=100000; %maxtimesteps
step=100; %how many timesteps of interest
tval=unique(round(linspace(1,tmax,step))); %divies tmax up in step amount unique whole numbers for indexing
%tredslump returns length(tval) depth array with Np 3dparticles picked out of tmax steps of
%3drandomwalk from origin. Limited by radius of Rk.
%pBook to be read ParticleBook
[pBook]=tredslump(Np,Rk,tmax,tval);
%plots the "pages" of pBook timesteps
for i=1:length(tval);
%makes vector corresponding to dimensions
X=(pBook(:,1,i));
Y=(pBook(:,2,i));
Z=(pBook(:,3,i));
%plots points accordingly, axis = constraints+1 to allow sphere and som
%margin
plot3(X,Y,Z,'kO');
axis([-(Rk+1) (Rk+1) -(Rk+1) (Rk+1) -(Rk+1) (Rk+1)]);
%trivial
grid on
hold on
drawnow
%arbitrary pause
pause(0.3);
hold off
end
The Function
function [Out]=tredslump(Np,Rk,tmax,tval)
%NP particles do a 3D randomwalk from origin over tmax timesteps. Limited
%by Rk*unitsphere.
%tval is a vector of length<=tmax that will decide wether or not a timestep
%is of interest (as to prevent us returning an array with
%depth=10.^(a-shit-ton)) Out is a Np*3 matrix where Out(pIndex,:) has
%x,y,z-coordinates for particle pIndex
w=1; %counter for later if
r=1; %radius for unitsphere random steps
dimR=3; %we 3 dimensions
pPos=zeros(Np,dimR); %startpositions = zeros
Out=zeros(Np,dimR,length(tval)); %here we save outdata Out(:,:,i) corresponds to tval(i);
for time=1:tmax
pIndex=randi(Np); %choose random particle to work on
theta=2*pi*rand(1); %generate random polar coordinates for move
phi=1/(cos(2*rand(1)-1));
%convert to cartesian coordinates
movex=r*cos(theta)*sin(phi);
movey=r*sin(theta)*sin(phi);
movez=r*cos(phi);
%make vector of coordinates
move=[movex,movey,movez];
%if position + move is within Rk-constraints add move to selected particle
if Rk > norm(pPos(pIndex,:)+move)
pPos(pIndex,:)=pPos(pIndex,:)+move;
end
%if we're at an timestep of interest then save data to new page in Out
if 1==ismember(time,tval);
Out(:,:,w)=pPos;
w=w+1;
end
end

Antworten (1)

John D'Errico
John D'Errico am 3 Mär. 2019
Bearbeitet: John D'Errico am 3 Mär. 2019
Confusing as hell. Sorry, but it is. The problem is, you state this is a 3-d random walk, apparently subject to constraints that the points lie within a unit sphere. No problem with that, at least in theory. But you never state what steps are allowed. And then you have a crazy scheme to generate the steps. Sorry, but you do, and I'm not positive where you came up with that scheme, partially because you never state sufficient information about the random walk to know if what you are doing has even a shred of mathematical justification behind it. I don't think it does. But then, only you know what kind of walk this is, and that has been kept a secret.
So all I can really do is look at your code. Looking...
You start out with 400 particles, all at the center of the sphere. Then you move one particle, chosen at random. So essentially, you are running 400 parallel random walks. No real problem with that, except that it is inefficient as hell to move only one point at a time, but your computer, your big cup of coffee.
Next, for the chosen particle to be CONSIDERED for a move, you pick a location somewhere on the surface of the unit sphere. It is not a uniformly distributed point on that sphere. But then why should that be important? ;-) We don't know why you are doing things this way yet. So where is the point chosen? If we assume theta is interpreted as a longitudonal angle, then you picked some point randomly on the interval [0,2*pi]. So anywhere around the sphere. But at what lattitude? That part is interesting.
The second coordinate in a spherical coordinate system would logically be distributed to lie in the interval [-pi/2,pi/2]. Not uniformly so, because there should be more points near the equator. I suppose one might be able to derive the necessary distribution. I might even do so along the way. But lets see what you got.
phi=1./(cos(2*rand(1,10000)-1));
hist(phi,100)
Hmm. Not even remotely what I might expect. So this is your first problem. But not your only problem.
Next, given that wrongly chosen point on the surface of the sphere, you now form the sum of that coordinate with the (x,y,z) coordinates of some point inside the sphere. If the result of that sum is still inside the sphere, you accept the move, otherwise, you reject it. Ok, that does keep you always inside the sphere, so good in that respect.
Sigh. WHAT? WHY?
So, anyway, It is obvious as to why your points tend to drift to the north pole. You have an insane way of choosing the step in your random walk, that is not remotely uniformly distrbuted in direction. Sorry for calling it insane. But there is some element of that in your choice. ;-) Perhaps you just misinterpreted some scheme you found to generate points on the surface of a sphere. (There are WAY easier ways to generate uniformly distributed points on the surface of a sphere. But I'm not even positive why you are writing your random walk in this way.)
Next, you have some scheme where this random walk seems to involve time. That too is not at all clear what you are doing, or why you did it that way. Sorry. But all I have to go on is your code, and the somewhat confusing comments in the code, as I try to reconcile the code you wrote with the limited description you gave. Hey, I love that you did use fairly frequent comments in the code. Otherwise I'd be totally in the dark. Oh, wait, I still feel like the lights are pretty dim here.
So before I go too far in this quest, I need to understand what the goal is here. What kind of random walk is this? For example, I would point out that depending where one of your particles lies, the next step is not uniformly distributed in either direction or in step length. Is that your goal? You need to provide some help in these things, that is, if you want help, at least from me.
  3 Kommentare
John D'Errico
John D'Errico am 3 Mär. 2019
Bearbeitet: John D'Errico am 3 Mär. 2019
Ok. Again, I'd probably just move all particles at the same time. But you can move one at a time, choosing them randomly. I'm still a little confused about how you are using time here, so I'll simplify that aspect.
So, you might try this:
Rk = 3;
Np = 400; %number of particles
Rk = 3; %radius on sphere
timesteps = 10000;
XYZ = zeros(Np,3); % each point is one row of XYZ
for t = 1:timesteps
move = randn(1,3);
move = move/norm(move);
ind = randi(Np,1);
if norm(XYZ(ind,:) + move) <= Rk
XYZ(ind,:) = XYZ(ind,:) + move;
end
end
[Xs,Ys,Zs] = sphere(100);
Hs = surf(Xs*Rk,Ys*Rk,Zs*Rk);
axis equal
box on
Hs.FaceAlpha = 0.25;
shading interp
hold on
plot3(XYZ(:,1),XYZ(:,2),XYZ(:,3),'o')
See how I generated a random direction vector, of length 1. I used the rotational symmetry of the n-dimensional normal distribution to generate a vector in a fully random direction. Then by normalizing the vector to have unit length, it becomes a random unit vector. As well, norm gives the test to see if the point would have fallen outside the Rk-sphere.
It might have been fun, in the case where a point would have moved outside the sphere, to then reflect the move to have it bounce off the sphere wall, but making the total distance traveled still one unit. ;-) A minor added complexity, but still should be eminently doable.
I'll let you figure out what you intended with time.
Filip Berntsson
Filip Berntsson am 4 Mär. 2019
Thanks a bunch! This makes a whole lot of sense to me. You've helped a lot!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Vehicle Scenarios finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by