Converting a problem from Mathematica to MATLAB

I have this problem in mathematica and want to do it in MATLAB.
steps[m_] := Table[2 RandomInteger[] - 1, {m}]
Walk1D[n_] := FoldList[Plus, 0, steps[n]]
LastPoint1D[n_] := Fold[Plus, 0, steps[n]]
nsteps = 200; nsq = Floor[Sqrt[nsteps]];
MeanSquareDistance1D[n_Integer, m_Integer] :=
N[Sum[LastPoint1D[n]^2, {m}]/m]
r2D=MeanSquareDistance1D[100, 1000]
data = Map[({#, MeanSquareDistance1D[#,2000]})&,
Range[10, 90, 20]]
This is what i did :
steps=@ (m) randi(3,1,m)-2;
Walk1D =@ (n) cumsum(steps(n));
LastPoint1D = @ (Walk1D) (Walk1D(end));
nsteps=200;
nsq=floor(sqrt(nsteps));
MeanSquareDistance1D= @ (n,m) sum((LastPoint1D(n)).^2)./m;
r2D=MeanSquareDistance1D(100,1000)
data=zeros(5,2);
for i=10:20:90
data=[i,MeanSquareDistance1D(i,2000)]
end
The problem is 1) r2D gives me the same value as many times as i run the code,but it must change. 2) In the "data " the first column its ok (its 10:20:90) but the 2nd column gives me the same numbers as i run the code. And,also,i don't know if i defined it right(the "data")
EDITED CODE -------------------------
steps=@ (m) 2*randi([0,1],[1,m])-1;
Walk1D =@ (n) [0,cumsum(steps(n))];
findend=@ (x) x(end);
LastPoint1D=@(n) findend(Walk1D(n));
nsteps=200;
nsq=floor(sqrt(nsteps));
MeanSquareDistance1D= @ (n,m) m.*sum((LastPoint1D(n)).^2)./m;
r2D=MeanSquareDistance1D(100,1000)
data=[ ];
for i=10:20:90
data=[data; i , MeanSquareDistance1D(i,2000)]
end
EDIT-->>>
I did it like this(with help):
steps1=@ (n, m) randi([-1 1], n, m);
LastPoint_1D=@ (n, m) sum(steps1(n, m));
MeanSquareDistance1D = @(n,m) mean(LastPoint_1D(n,m).^2);

2 commentaires

Oleg Komarov
Oleg Komarov le 21 Fév 2011
Can you comment the code in mathematica?
George
George le 21 Fév 2011
Random walks in one dimens.
"step"->examines trajectories which consists from m steps
"walk1d"->defines all the intermediate positions
"lastpoint"->defines only the last position
Then ,i am trying to define the mean square distance between initial and final position of the trajectory and final
"data"->dependence of the mean square distance from the number of steps

Connectez-vous pour commenter.

Réponses (2)

Seth DeLand
Seth DeLand le 21 Fév 2011
George,
Looks like there's a problem with this line of code:
LastPoint1D = @ (Walk1D) (Walk1D(end));
I believe what you are trying to do is call the 'Walk1D' function and find the last element of the vector that is returned by 'Walk1D'. Here is one way to do that:
findend = @(x) x(end);
LastPoint1D = @ (n) findend(Walk1D(n));
The 'findend' function just takes in a vector and returns the last element of that vector. Try replacing your line of code above with the two lines I suggested and let me know if that helps.

12 commentaires

George
George le 21 Fév 2011
Hello,still the same problem and also some warnings:"input arguments must be scalar".I must mention that when i run LastPoint1D ,i don't have problem.The number changes values.The problem is with r2D and "data".Thanks!
Seth DeLand
Seth DeLand le 21 Fév 2011
George,
I guess I'm a little confused. The reason I though there with that line of code is because it doesn't actually call the 'Walk1D' function. What it actually does is create a function handle 'LastPoint1D' that has 1 input argument 'Walk1D'. The function then takes your input argument and returns the last element of it. At no point is the function 'Walk1D' actually called. When I run the code with the 2 lines I mentioned above I think we're on the right track:
steps=@ (m) randi(3,1,m)-2;
Walk1D =@ (n) cumsum(steps(n));
findend = @(x) x(end);
LastPoint1D = @ (n) findend(Walk1D(n));
nsteps=200;
nsq=floor(sqrt(nsteps));
MeanSquareDistance1D= @ (n,m) sum((LastPoint1D(n)).^2)./m;
r2D=MeanSquareDistance1D(100,1000)
data=zeros(5,2);
for i=10:20:90
data=[i,MeanSquareDistance1D(i,2000)]
end
I get different values for 'r2D' and 'data' each time I run the code. I'm also not sure about the warning messages you're getting, I don't get that message when I run your code. Could you post the code you are running when you get the warning message?
George
George le 22 Fév 2011
Hello,with the code you gave me, now i have different values for r2D but as many times as i execute the code are very small (10^-2 - 10^-3).In the original code (for mathematica ) are in order of 10 - 100.Also,i want data to be a matrix 5x2 and display it.In the way we fill it,it gives 5 different "data" matrices.So,we must fill it somehow in a different manner.And last,when i plot "data" it gives me the right graph but it adds a horizontal line of points on to the x-axis(i don't want this).I tried "plot(data(:,1),data(:,2))"but it gives me wrong graph.
George
George le 22 Fév 2011
EDIT--->> For the small values i found it .I must do "MeanSquareDistance1D= @ (n,m) m*sum((LastPoint1D(n)).^2)./m;".I forgot to multiply with "m".The other problems still exist..
George
George le 22 Fév 2011
Also,the right is :steps=@ (m) 2*randi([0,1],[1,m])-1;
George
George le 22 Fév 2011
I think i tracked the problem but i can't express it.I want the Walk1D and the LastPoint1D to begin always with zero!
For example,as many times as i execute the code,the Walk1D[10] must give me:
{0, -1, 0, 1, 2, 1, 2, 3, 4, 3, 4}
{0, 1, 2, 1, 2, 1, 0, 1, 0, -1, -2} and so on.
But ,in my code this doesn;t happen.
I have: Walk1D =@ (n) cumsum(steps(n));
I don't know how to do it.If i try "Walk1D =@ (n) cumsum(0:steps(n));"
it doesn't work..
George
George le 22 Fév 2011
I did this :Walk1D =@ (n) [0,cumsum(steps(n))]; but i don't know if its right(it didn't fixed the problem though)
Seth DeLand
Seth DeLand le 22 Fév 2011
George, sounds like you're getting close. You've made a lot of changes since I last looked at the code, any chance you could post the most recent version and we could go from there?
George
George le 22 Fév 2011
I edited my code.Please check it.Somewhere in the sum maybe the problem but i can't figure
George
George le 22 Fév 2011
The problem is somewhere in the LastPoint1D.If instead of this , write " m.*sum((steps(n)).^2) .it gives me the 2nd column(of "data") with numbers "10,30,50,70,90".But i want to give me numbers close to these,as i write above.
George
George le 23 Fév 2011
I edited my post.It's ok now!
George
George le 23 Fév 2011
I wanted to ask you..If i had the LastPoint2D(n,m)
How could i write the equivalent of "findend = @(x) x(end);"
Thank you

Connectez-vous pour commenter.

Oleg Komarov
Oleg Komarov le 22 Fév 2011
I found this article "One Dimensional RW", and referring to the paragraph "The root mean square distance from the origin after a random walk of n unit steps is ?n.", I derived the following function:
function Out = MeanSquareDistance1D(n,m)
% Create random walk with unit steps
Walk1D = [0 cumsum(2*randi([0,1],[1,m])-1)];
% Plot it
% plot(Walk1D)
% Means square distances from the origin for all steps
Out = Walk1D.^2./(1:m+1);
% Select just the steps at n intervals
Out = Out(n:n:m);
end
Or in handle fmt:
Walk1D = @(m) [0 cumsum(2*randi([0,1],[1,m])-1)];
MeanSquareDistance1D = @(n,m) Walk1D(m).^2./(1:m+1);
Oleg

2 commentaires

George
George le 22 Fév 2011
First of all thanks for searching!I tried it but it gives me totally wrong results.The "data" matrix become too big.I tried "MeanSquareDistance1D = @(n,m) Walk1D(n).^2./(1:m+1);"
but it gives me :matrix dimensions must agree.
George
George le 23 Fév 2011
I edited my post.It's ok now!

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Question posée :

le 21 Fév 2011

Community Treasure Hunt

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

Start Hunting!

Translated by