Self avoiding random walk

조회 수: 1 (최근 30일)
Uffe Larsen
Uffe Larsen 2015년 2월 3일
편집: Walter Roberson 2017년 4월 27일
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
Gio Sco
Gio Sco 2017년 4월 27일
Did you find a solution to your problem at the end?

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2015년 2월 3일
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
Image Analyst
Image Analyst 2015년 2월 3일
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).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by