http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840
MATLAB Central Newsreader  Matlab 3d Walk problem
Feed for thread: Matlab 3d Walk problem
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 19 Nov 2010 01:16:03 +0000
Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#797526
Justin Twardowski
Hi everyone. So, I'm currently taking a physics course which happens to involve a computer project; the project is a statistical analysis of a 3dimensional random walk (each step being the same length and random in x,y,and z orientations). I'm fairly new to Matlab (I originally learned programming a couple years ago in C, but most of that has since been forgotten). <br>
<br>
Anyways, here is a description of what I have so far...<br>
<br>
To begin the code, I started by creating a unit vector (I used spherical coordinates for the unit because I was able to set r=1, and I then decomposed the vector for its x, y and z components). <br>
<br>
The problem...I am able to create my randomly oriented unit vectors, and I am assuming since it is inside of a "for" loop to account for all N steps. Right now, my code returns a list of the vectors that compose each step, I just need to figure out how to add them. Once I get this part, the statistical analysis comes into play, which should then be no problem. <br>
<br>
Does anybody have an idea of how to do this, or a way around it?

Fri, 19 Nov 2010 01:50:05 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#797539
Darren Rowland
Something like this perhaps, where the positions visited are recorded, rather than the steps.<br>
<br>
position = zeros(3,N+1);<br>
for k = 2:N+1<br>
step = 2*rand(3,1)1; % Create a random step in (x,y,z)<br>
step = step/norm(step); % Normalise to unit length<br>
position(:,k) = position(:,k1) + step; % Add new step to last position<br>
end<br>
<br>
Hth<br>
Darren

Fri, 19 Nov 2010 02:13:04 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#797543
Justin Twardowski
Okay, so I understand conceptually what you mean, but I get lost in the code. Right now, here is what I have:<br>
<br>
<br>
%This program creates a random unit vector in 3D. It then sums over many of<br>
%these unit vetors in order to create a total displacement, R. Once it has<br>
%found R, it then creates many more displacement vectors and finds the average displacement, R.<br>
clear all<br>
<br>
<br>
for n = 1:1000<br>
%this repeats for 1000 random walks<br>
<br>
for N=1:100<br>
%accounts for 100 steps<br>
<br>
r=1;<br>
phi=rand(1)*pi;<br>
theta=rand(1)*2*pi;<br>
%creates one 3d unit vector randomly oriented<br>
<br>
Rx= r*sin(phi)*cos(theta);<br>
Ry= r*sin(phi)*sin(theta);<br>
Rz= r*cos(phi);<br>
%breaks down vector into x,y,z components<br>
<br>
R=[Rx;Ry;Rz];<br>
%returns random unit vector for each of 100 steps<br>
<br>
end<br>
<br>
end<br>
end<br>
<br>
<br>
I'm not sure exactly how to incorporate what you said. My attempt to try and incorporate a Rxnew=Rxlast+Rx, but I couldn't get the labelling right (I think this is similar to what you said about keeping track of present position. Then, my next thought was that maybe I could somehow return the result vectors from the loop to create a matrix and then sum over all x's, y's and z's to find net displacement, but I can't figure out a way to do that w/the loop present. Either way, if you could help enlighten/walk me through this a little, it would be greatly appreciated. Thank you.

Fri, 19 Nov 2010 02:34:05 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#797544
Justin Twardowski
Nevermind, what you posted makes perfect sense and is a lot more efficient than what I had. Thank you.

Fri, 19 Nov 2010 06:03:03 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#797587
Darren Rowland
No problem. Here is an alternative method which forms the random steps, then uses cumsum to get the resultant positions<br>
<br>
N = 10;<br>
steps = 2*rand(3,N)1; <br>
steps = steps./repmat(sqrt(sum(steps.^2,1)),3,1); % Normalize to unit length<br>
position = cumsum([zeros(3,1) steps],2); % Obtain set of positions, <br>
% assuming starting at the origin

Fri, 19 Nov 2010 19:09:04 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#797760
Justin Twardowski
That method seems better. For this project, I'm supposed to run through data sets for the number of steps being 100, 1000, 10000 and 100000 so it was taking a while to run. MY question now is, would there be a way to just return the final value instead of the value after each iteration? That would be much easier to work with when I have to run my data analysis.

Fri, 19 Nov 2010 19:23:03 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#797766
Sean de
"Justin Twardowski" <jtwardow@email.arizona.edu> wrote in message <ic6i0g$o8e$1@fred.mathworks.com>...<br>
> That method seems better. For this project, I'm supposed to run through data sets for the number of steps being 100, 1000, 10000 and 100000 so it was taking a while to run. MY question now is, would there be a way to just return the final value instead of the value after each iteration? That would be much easier to work with when I have to run my data analysis.<br>
<br>
So instead of using cumsum, which updates at each step, just use sum to get the end result?

Fri, 19 Nov 2010 23:00:08 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#797816
Roger Stafford
"Darren Rowland" <darrenjremovethisrowland@hotmail.com> wrote in message <ic53un$jf2$1@fred.mathworks.com>...<br>
> .......<br>
> steps = 2*rand(3,N)1; <br>
> ........<br>
          <br>
If the direction of each step is to be uniformly distributed in threedimensional orientation, it would be better to use the randn function:<br>
<br>
steps = randn(3,N);<br>
position = cumsum(bsxfun(@rdivide,steps,sqrt(sum(steps.^2,1))),2);<br>
<br>
It can easily be shown that this method with randn results in a statistically uniform distribution spherewise, whereas 2*rand(3,N)1 gives a degree of preference to the eight corner directions and therefore depends on how the xyz axes are oriented. To see this, imagine a cube with a tangential sphere inside and an orthogonal projection of the cube's volume onto the surface of this sphere. The quantity 2*rand(3,N)1 fills the inside of a 2x2x2 cube uniformly. Therefore the eight corner directions would receive a somewhat higher density on the spherical surface (by a factor of 3*sqrt(3) in fact) than the directions orthogonal to the six faces.<br>
<br>
Roger Stafford

Sun, 21 Nov 2010 01:41:03 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#797991
Justin Twardowski
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ic6vho$3rn$1@fred.mathworks.com>...<br>
> "Darren Rowland" <darrenjremovethisrowland@hotmail.com> wrote in message <ic53un$jf2$1@fred.mathworks.com>...<br>
> > .......<br>
> > steps = 2*rand(3,N)1; <br>
> > ........<br>
>           <br>
> If the direction of each step is to be uniformly distributed in threedimensional orientation, it would be better to use the randn function:<br>
> <br>
> steps = randn(3,N);<br>
> position = cumsum(bsxfun(@rdivide,steps,sqrt(sum(steps.^2,1))),2);<br>
> <br>
<br>
<br>
Hi Roger, <br>
<br>
Thank you for your contribution; seeing as this problem is a statistical analysis, it's reassuring to know I now have the proper distribution to work with. Anyways, I've been looking over the second line of code you've posted, and am struggling to understand and modify it to suit my needs. Would you be able to walk me though the process going on there? I understand that the first line is generating an array of random values, and I'm guessing that the normalization is taken into consideration in the second line? Also, I've been struggling to write this code in a way that returns me what I think I need for my analysis, and that is an array of the displacements that I can work with by adding, etc with the elements to find displacement and other quantities.

Sun, 21 Nov 2010 01:55:04 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#797993
Justin Twardowski
I thought I should specify how I need my data to be returned. The other main quantity being examined is the mean displacement squared; that's why I'm assuming an array of the vector steps would be nice to work with. Thanks to everyone, again.

Sun, 21 Nov 2010 04:59:05 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#798009
Roger Stafford
"Justin Twardowski" <jtwardow@email.arizona.edu> wrote in message <ic9u5o$bkb$1@fred.mathworks.com>...<br>
"Justin Twardowski" <jtwardow@email.arizona.edu> wrote in message <ic9tbf$jqe$1@fred.mathworks.com>...<br>
>> Thank you for your contribution; seeing as this problem is a statistical analysis, it's reassuring to know I now have the proper distribution to work with. Anyways, I've been looking over the second line of code you've posted, and am struggling to understand and modify it to suit my needs. Would you be able to walk me though the process going on there? I understand that the first line is generating an array of random values, and I'm guessing that the normalization is taken into consideration in the second line? Also, I've been struggling to write this code in a way that returns me what I think I need for my analysis, and that is an array of the displacements that I can work with by adding, etc with the elements to find displacement and other quantities. <br>
> I thought I should specify how I need my data to be returned. The other main quantity being examined is the mean displacement squared; that's why I'm assuming an array of the vector steps would be nice to work with. Thanks to everyone, again. <br>
        <br>
That code is easier to discuss if I replace it with the equivalent 'repmat' version instead of using 'bsxfun' and break it up into individual temporary arrays.<br>
<br>
steps = randn(3,N);<br>
t1 = sum(steps.^2,1);<br>
t2 = sqrt(t1);<br>
t3 = repmat(t2,3,1);<br>
steps = steps./t3;<br>
position = cumsum(steps);<br>
<br>
The first step creates a threedimensional multivariate Gaussian distribution of points with mean zero and standard deviation one for each coordinate in a 3 by N array. It is easy to show that the probability density is dependent only on the distance from the origin and not the rotational angles, since randn values are statistically independent.<br>
<br>
The second step computes the sum of the squares of each column resulting in a 1 by N row vector. The third step takes the square root of these, obtaining the distance from the origin of each of the points, and the vector remains 1 by N. The fourth step makes duplicates of this distance row vector, resulting in a 3 by N array in which all three rows are the same.<br>
<br>
The fifth step does normalization. It amounts to a projection of each point onto the unit sphere about the origin. Each coordinate of a point is divided by the point's distance from the center, so the adjusted point is now perforce a unit distance from the center and therefore lies on the unit sphere. The previous 'repmat' operation was needed so the the "./" operation would have operands of the same size to work with. Using bsxfun instead of repmat is a way of creating a smarter division process that carries out the same operation. At this point each column of 'steps' is an individual change of position of magnitude one taken in a random direction.<br>
<br>
The final sixth step computes the cumulative vector sum of these steps starting with the first step, then the sum of the first two steps, then the sum of the first three steps, and so forth until N steps have been taken. The variable 'position' is therefore also a 3 by N array with each column representing the resulting position in 3D starting at the origin, after N steps. The last column gives the final position.<br>
<br>
I am rather puzzled by what you mean by "mean displacement squared". Each step above has a displacement magnitude of one and the mean of that squared would of course also be one. Perhaps you refer to the mean of the squares of each of the coordinate increments, or perhaps the mean of the square of the final distance from the start point over many experiments? Whatever your desired computation it seems very likely you can obtain it from some simple operation on either 'steps' or 'position'.<br>
<br>
Roger Stafford

Sun, 21 Nov 2010 16:38:03 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#798071
Justin Twardowski
Hi Roger, <br>
<br>
Thanks again. This project is due tomorrow and today is the day I hopefully finish this up. Thank you for your explanation; it helped me understand that better (I am still unfamiliar with certain commands, their uses and syntax). Anyways, I'm going to try applying what you said and see if I could get it to work. <br>
In terms of the mean squared displacement, which I failed to explain. In my simulation, I run walks of 100, 1000, 10000, and 100000 steps, and repeat those each 1000 times to find my average displacement after N steps, n walks. The mean squared displacement that I'm supposed to find and evaluate is with regards to the 1000 repeated trials per the given numbers of steps (so this is repeated 4 times). In this case, the distance is just 1 so it seems trivial, but I'm still supposed to "show it" via the code.

Sun, 21 Nov 2010 18:42:03 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#798093
Justin Twardowski
Hi, so I've been trying to get a start with the code Roger posted, but am having trouble summing over the displacement positions over 1000 walks to get an average displacement. Here is the bare structure of what I have; I just can't figure out the right expression to add up the displacements in my second innermost loop. <br>
<br>
P.S. this is probably an elementary question, it's just been a long time since I've last programmed so this is fairly rusty; thanks for bearing with me<br>
<br>
<br>
<br>
<br>
clear al<br>
l<br>
for k=2:5 <br>
for n=1000 <br>
for N=10^k<br>
<br>
steps = randn(3,N);<br>
%creates random array for use as vectors<br>
<br>
t1 = sum(steps.^2,1);<br>
t2 = sqrt(t1);<br>
t3 = repmat(t2,3,1);<br>
steps = steps./t3;<br>
%normalizes<br>
<br>
R = sum(steps,2)<br>
%sums over steps to return total displacement vector for one walk<br>
<br>
Rsq=norm(R)^2<br>
%returns total displacement squared for one walk<br>
<br>
<br>
end<br>
%this is the space I feel my expression should be to add the net displacements from the innermost loop (which represents position after one walk)<br>
<br>
end <br>
end

Sun, 21 Nov 2010 19:54:04 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#798113
Justin Twardowski
bump

Sun, 21 Nov 2010 22:37:03 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#798136
Justin Twardowski
Anyone? I feel like my information is being outputted in the wrong fashion for me to be able to work with and sum over the 1000 repeated walks.

Mon, 22 Nov 2010 06:06:03 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#798187
Darren Rowland
There are a few ways you might do it but here is one which  I think  does what you want.<br>
<br>
function avgDispK = randWalk3D<br>
numTrials = 1000;<br>
avgDispK = zeros(4,1);<br>
for k = 2:5<br>
N = 10^k;<br>
totalDisplacement = 0;<br>
for n = 1:numTrials<br>
<br>
steps = randn(3,N);<br>
%creates random array for use as vectors<br>
<br>
t1 = sum(steps.^2,1);<br>
t2 = sqrt(t1);<br>
t3 = repmat(t2,3,1);<br>
steps = steps./t3;<br>
%normalizes<br>
<br>
R = sum(steps,2);<br>
%sums over steps to return total displacement vector for one walk<br>
<br>
Rsq = norm(R)^2;<br>
%returns total displacement squared for one walk<br>
<br>
totalDisplacement = totalDisplacement + Rsq;<br>
end<br>
avgDispK(k1) = totalDisplacement/numTrials;<br>
end

Wed, 06 Nov 2013 13:39:08 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#913690
Vivekananda Bal
Hi everybody, I am presently working on lattice modelling, where we have to move aparticle in 3D randomly along the sides of a cubic lattice. I want to create a random unit vector along the lattice (6 possible ways of moving the particle). I am little bot confused about it. Can you guys please suggest me a suitable way?<br>
<br>
<br>
<br>
"Justin Twardowski" wrote in message <ic4mfg$r55$1@fred.mathworks.com>...<br>
> Okay, so I understand conceptually what you mean, but I get lost in the code. Right now, here is what I have:<br>
> <br>
> <br>
> %This program creates a random unit vector in 3D. It then sums over many of<br>
> %these unit vetors in order to create a total displacement, R. Once it has<br>
> %found R, it then creates many more displacement vectors and finds the average displacement, R.<br>
> clear all<br>
> <br>
> <br>
> for n = 1:1000<br>
> %this repeats for 1000 random walks<br>
> <br>
> for N=1:100<br>
> %accounts for 100 steps<br>
> <br>
> r=1;<br>
> phi=rand(1)*pi;<br>
> theta=rand(1)*2*pi;<br>
> %creates one 3d unit vector randomly oriented<br>
> <br>
> Rx= r*sin(phi)*cos(theta);<br>
> Ry= r*sin(phi)*sin(theta);<br>
> Rz= r*cos(phi);<br>
> %breaks down vector into x,y,z components<br>
> <br>
> R=[Rx;Ry;Rz];<br>
> %returns random unit vector for each of 100 steps<br>
> <br>
> end<br>
> <br>
> end<br>
> end<br>
> <br>
> <br>
> I'm not sure exactly how to incorporate what you said. My attempt to try and incorporate a Rxnew=Rxlast+Rx, but I couldn't get the labelling right (I think this is similar to what you said about keeping track of present position. Then, my next thought was that maybe I could somehow return the result vectors from the loop to create a matrix and then sum over all x's, y's and z's to find net displacement, but I can't figure out a way to do that w/the loop present. Either way, if you could help enlighten/walk me through this a little, it would be greatly appreciated. Thank you.

Wed, 06 Nov 2013 15:06:51 +0000
Re: Matlab 3d Walk problem
http://www.mathworks.com/matlabcentral/newsreader/view_thread/296840#913698
Steven Lord
<br>
"Vivekananda Bal" <124026003@iitb.ac.in> wrote in message <br>
news:l5dgps$8$1@newscl01ah.mathworks.com...<br>
> Hi everybody, I am presently working on lattice modelling, where we have <br>
> to move aparticle in 3D randomly along the sides of a cubic lattice. I <br>
> want to create a random unit vector along the lattice (6 possible ways of <br>
> moving the particle). I am little bot confused about it. Can you guys <br>
> please suggest me a suitable way?<br>
<br>
Generate two random numbers. The first is one of 1, 2, or 3 and represents <br>
the dimension in which the particle walks. The second is +1 or 1 and <br>
represents the direction the particle walks in that dimension. You can use <br>
RANDI to do this; the second number will require a tiny amount of <br>
adjustment. [If the particle can stand still, that would eliminate the need <br>
to adjust RANDI's output for the second case.]<br>
<br>
If your lattice is finite, you will need to handle the case where you're at <br>
the edge of the cube.<br>
<br>
Rather than calling RANDI twice at each step to generate scalar values, I'd <br>
generate a large number of steps ahead of time with two RANDI calls then use <br>
the steps as you need them.<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>