Self avoiding random walk
Show older comments
I need to program a self avoiding random walk, and find the squared mean distance from start to end. I have made the random walk, but as soon as the walk is to long or is repeated to often, it fails. Matlab kinda freezes. I need the chain to be up to 10.000 steps long and repeated at least 100 times, is i my computer that is the problem or is it something else? and how do i fix it?
kind regards
distressed student.
function R=saw(N)
d=zeros(1,N);
n=2;
x=zeros(1,N);
y=zeros(1,N);
while n<=N
d(n)=rand;
if d(n)<0.25
x(n)=x(n-1)-1;
y(n)=y(n-1);
elseif d(n)<0.5
x(n)=x(n-1)+1;
y(n)=y(n-1);
elseif d(n)<0.75
y(n)=y(n-1)+1;
x(n)=x(n-1);
elseif d(n)<1
y(n)=y(n-1)-1;
x(n)=x(n-1);
end
r=[x(1:n);y(1:n)]';
ur=unique(r,'rows');
if size(ur)==size(r)
n=n+1;
end
end
R2=x(end)^2+y(end)^2;
R=sqrt(R2);
1 Comment
Gio Sco
on 27 Apr 2017
Did you find a solution to your problem at the end?
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!