📄 gas.m
字号:
%gas.m/created by PJNahin for "Duelling Idiots"(5/22/99)
%This m-file simulates the diffusion of gas molecules in a sealed
%container by using the Ehrenfest ball exchange rules. The simulation
%starts with n molecules (i.e., balls) of one type (i.e., black) on
%one side of the container, and n more molecules of another type (i.e.,
%white balls) on the other side. The two urns play the roles of the
%two sides of the container. To simulate the ball (molecule)
%movements, the program selects two random numbers from 0 to 1, which
%are then compared to the current probabilities of selecting a black
%ball from urn I and a white ball from urn II. If BOTH random numbers
%are greater than these two probabilities then a white ball has been
%selected from urn I and a black ball has been selected from urn II,
%and so the number black balls in urn I is increased by one while the
%number of white balls in urn II is increased by one. If BOTH random
%numbers are less than or equal to these two probabilities then a
%black ball has been selected from urn I and a white ball has been
%selected from urn II and so the number of black balls in urn I is
%decreased by one while the number of white balls in urn II is decreased
%by one. If one of the random numbers is greater than its corresponding
%probability while the other random number is less than its
%corresponding probability, then no action is taken because then a
%white (black) ball moves from urn I to urn II at the same time a white
%(black) ball moves in the opposite direction. That is, there is no
%net change. Then, the ball selection probabilities are recalculated and
%another ball exchange is simulated.
%
%
rand('state',100*sum(clock)) %new seed for the generator;
n=50; %number of balls in each urn;
nb1=n; %number of black balls INITIALLY in urn I;
nw2=n; %number of white balls INITIALLY in urn II;
pb1=nb1/n; %probability of selecting a black ball from urn I;
pw2=nw2/n; %probability of selecting a white ball from urn II;
for trials =1:600;
system(trials)=pb1;
ball1=rand;
ball2=rand;
if(ball1>pb1&ball2>pw2) %white ball selected from urn I
nb1=nb1+1; %and black ball selected from
nw2=nw2+1; %urn II;
elseif(ball1<=pb1&ball2<=pw2) %black ball selected from urn I
nb1=nb1-1; %and white ball selected from
nw2=nw2-1; %urn II;
end
pb1=nb1/n;
pw2=nw2/n;
end
plot(system)
axis([1 trials 0 1])
grid
xlabel('time, in arbitrary units')
ylabel('fraction of balls in urn I that are black')
title('Fig.9.1 - Simulation of the Ehrenfest Ball Exchange Process')
figure(1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -