我在分区箱模拟中具有2D理想气体。最初,所有气体颗粒都位于盒子的左侧。然后随着时间的推移,它们开始在两边不断移动。
这是我的代码:
N=20; %total number of particles
nstep = 100;
n = randi(nstep,1);
n(1) = N; % initial conditions- number of particles on left side
for i = 2:nstep_5
r = rand(1,1);
if (r<n(i-1)/N)
n(i) = n(i-1) - 1; % Move atom from left to right
else
n(i) = n(i-1) + 1; % Move atom from right to left
end
end
time=(1:nstep)
我需要他们表现出来,因为有60%的概率从左移到右,而有40%的概率从右移到左。
我正在努力在此代码中插入概率函数。有什么想法吗?
您无需检查粒子已经在左侧并再次离开的状态(即留在原处?还是与墙碰撞并向右移动?不清楚)。至于循环中的if条件,您可以将其编写如下:
if rand(1)<=0.6
% move left to right
else
% move right to left
end