Self avoiding random walk

1 visualizzazione (ultimi 30 giorni)
Uffe Larsen
Uffe Larsen il 3 Feb 2015
Modificato: Walter Roberson il 27 Apr 2017
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 Commento
Gio Sco
Gio Sco il 27 Apr 2017
Did you find a solution to your problem at the end?

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 3 Feb 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 Commento
Image Analyst
Image Analyst il 3 Feb 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).

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by