Self avoiding random walk

1 vue (au cours des 30 derniers jours)
Uffe Larsen
Uffe Larsen le 3 Fév 2015
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 commentaire
Gio Sco
Gio Sco le 27 Avr 2017
Did you find a solution to your problem at the end?

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 3 Fév 2015
The problem is that you walked yourself into an alleyway and you have no mechanism for backing up. You're basically trapped with no place to go. All the potential next step locations (up, down, left, and right) have been visited already. I think you should just set n=1 in that case and start fresh.
  1 commentaire
Image Analyst
Image Analyst le 3 Fév 2015
Another option, rather than resetting all the way to zero is to keep track of the last n that had more than two available options, and take the direction different than you chose the first time. For example if at step 76 you could go north or west and you chose north, but in step 85 you ended up in a dead end. But remember back at step 76 you had two ways you could have gone? So, set n=76 and pick a different direction (west instead of north).

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by