I'm coding a monte carlo simulation where a particle moves based on a vacancy next to it. I've written the step where a particle will move, but I'm unsure of how to code the for loop where in each step another random particle is selected and moves.
When i try to create the loop, i get the error that the right and left sides have a different number of elements.
Here is my code of the first step:
k=10;
n=k*0.5;
p=0.5;
x=1:k;
z=zeros(1,n,'uint32');
y=randsample(x,n);
ys=randsample(y,1);
find(x==ys+1);
find(x==ys-1);
Lia1=ismember(ys+1,y);
Lia2=ismember(ys-1,y);
Lia3=ismember(ys+1,x);
Lia4=ismember(ys-1,x);
if ys==1 && Lia2==0
yn=(y);
elseif ys==k && Lia1==0
yn=(y);
elseif Lia1==0
yn=[y(y~=ys) ys+1];
elseif Lia2==0
yn=[y(y~=ys) ys-1];
elseif Lia1==0 && Lia2==0
if flip <= p
yn=[y(y~=ys) ys+1];
elseif flip >= p
yn=[y(y~=ys) ys-1];
end
elseif Lia1==1 && Lia2==1
yn=(y);
end
figure
subplot(2,1,1); plot(y,z,'o')
axis([0 10 0 10])
subplot(2,1,2); plot(yn,z,'o')
axis([0 10 0 10])
4 Comments
Elizabeth Teeter (view profile)
이 댓글에 대한 바로 가기 링크
https://kr.mathworks.com/matlabcentral/answers/494899-for-loop-for-monte-carlo-code#comment_774579
Ridwan Alam (view profile)
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/494899-for-loop-for-monte-carlo-code#comment_774864
Elizabeth Teeter (view profile)
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/494899-for-loop-for-monte-carlo-code#comment_774871
Elizabeth Teeter (view profile)
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/494899-for-loop-for-monte-carlo-code#comment_775299
Sign in to comment.