在 MATLAB 中模拟排球比赛
Simulating a Volleyball game in MATLAB
我想模拟以下内容:
我们有两支排球队:
l = local One v = the Guests [= 30 = 30 = 30 = ]
假设L有18分,下一个得分的概率为p1=0.6,而V有20分,下一个得分的概率为p2=0.54。
到目前为止我试过的是这段代码:-
% Simulating a volleyball game
p1 = 0.6; % Introducing the probabilities of both teams
p2 = 0.54;
n=25; % The maximum number of points in a set
L=18;
V=20;
t1=(rand<p1);
t2=(rand<p2);
while n-L>0 || n-V>0
if t1==1
L=L+1;
elseif t2==1
V=V+1;
end
end
disp([L V])
问题是它似乎没有尽头,我想不出当一支球队得到 25 分时停止比赛的方法。另一个问题是我无法检查循环是否正常工作,因为程序不显示任何内容。我认为程序没有计算正确的 t1
和 t2
,因为它是用相同的随机数来计算的。
如何解决这些问题?
编辑:
我也想运行这10000次,计算每队的胜率!
首先,循环条件不正确。其次,在每次迭代中 t1
和 t2
的值不断增加。第三,当 t1
和 t2
都为零且循环将始终保持为真时,没有什么可以应对这种情况。第四,假设概率有误,因为概率之和必须等于1。
修改后的代码如下:
% The probabilities are incorrect but I'm leaving them as it is
p1 = 0.6;
p2 = 0.54;
n = 25; % The maximum number of points in a set
Result=zeros(1,1000); % Pre-allocation
for k=1:1000
L=18;
V=20;
while n-L>0 & n-V>0 % Notice the condition
t1 = (rand<p1);
t2 = (rand<p2);
if t1==1
L=L+1;
elseif t2==1
V=V+1;
end
end
Result(k)=L>V ; % Storing L's victories as 1 and V's victories as 0
end
Win_Percent_L = length(find(Result))/1000
Win_Percent_V = 1-Win_Percent_L
我想模拟以下内容:
我们有两支排球队:
l = local One v = the Guests [= 30 = 30 = 30 = ]
假设L有18分,下一个得分的概率为p1=0.6,而V有20分,下一个得分的概率为p2=0.54。
到目前为止我试过的是这段代码:-
% Simulating a volleyball game
p1 = 0.6; % Introducing the probabilities of both teams
p2 = 0.54;
n=25; % The maximum number of points in a set
L=18;
V=20;
t1=(rand<p1);
t2=(rand<p2);
while n-L>0 || n-V>0
if t1==1
L=L+1;
elseif t2==1
V=V+1;
end
end
disp([L V])
问题是它似乎没有尽头,我想不出当一支球队得到 25 分时停止比赛的方法。另一个问题是我无法检查循环是否正常工作,因为程序不显示任何内容。我认为程序没有计算正确的 t1
和 t2
,因为它是用相同的随机数来计算的。
如何解决这些问题?
编辑:
我也想运行这10000次,计算每队的胜率!
首先,循环条件不正确。其次,在每次迭代中 t1
和 t2
的值不断增加。第三,当 t1
和 t2
都为零且循环将始终保持为真时,没有什么可以应对这种情况。第四,假设概率有误,因为概率之和必须等于1。
修改后的代码如下:
% The probabilities are incorrect but I'm leaving them as it is
p1 = 0.6;
p2 = 0.54;
n = 25; % The maximum number of points in a set
Result=zeros(1,1000); % Pre-allocation
for k=1:1000
L=18;
V=20;
while n-L>0 & n-V>0 % Notice the condition
t1 = (rand<p1);
t2 = (rand<p2);
if t1==1
L=L+1;
elseif t2==1
V=V+1;
end
end
Result(k)=L>V ; % Storing L's victories as 1 and V's victories as 0
end
Win_Percent_L = length(find(Result))/1000
Win_Percent_V = 1-Win_Percent_L