⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chess.m

📁 一些常被用于教学或者参考的概率论的实例的源代码
💻 M
字号:
%chess.m/created by PJNahin for "Duelling Idiots"(5/17/98)
%This m-file computes the probabilities, in an N-game chess
%match, of the match ending in a tie, in a win for the champ, or
%in a win for the challenger. The probability the champ wins an
%individual game is p, and the probability an individual
%game ends in a tie is q.
%
%
N=input('Number of games in match? ')
q=input('Probability of a drawn game? ')
ANSWER=zeros(3,round(100*(1-q)));
for p=.01:.01:1-q
   g=(1-p-q)*p;
   RZ=zeros(1,N);        %RZ plays the role of 'row zero' in the             
   C=zeros(N);           %C(j,k), as in MATLAB all matrix indices
   for k=1:N             %start at one;
      C(k,k)=p^k;
   end
   for n=1:N
   if 2*round(n/2)==n
      for d=0:2:n
         top=(n+d)/2;
         bottom=(n-d)/2;
         I=binomial(n,bottom)*binomial(top,bottom);  %see Problem 6;
         I=I*(g^bottom)*(q^d);
         RZ(n)=RZ(n)+I;
      end
   else
      for d=1:2:n
         top=(n+d)/2;
         bottom=(n-d)/2;
         I=binomial(n,bottom)*binomial(top,bottom);
         I=I*(g^bottom)*(q^d);
         RZ(n)=RZ(n)+I;
      end
   end
end
g=1-p-q;
   for c=2:N
      k=1;
      n=c;
      while n<=N
         C(k,n)=C(k,n-1)*q+C(k+1,n-1)*g;
         if k==1
            C(k,n)=C(k,n)+RZ(n-1)*p;
         else
            C(k,n)=C(k,n)+C(k-1,n-1)*p;
         end
         k=k+1;
         n=n+1;
      end
   end
   SUM=0;
   for k=1:N
      SUM=SUM+C(k,N);
   end
   Q=round(100*(1-q));
   j=round(100*p);
   if j<=Q
      ANSWER(1,j)=RZ(N);
      ANSWER(2,j)=SUM;
      ANSWER(3,j)=1-ANSWER(1,j)-ANSWER(2,j);
   else
   end
end
%
%
p=.01:.01:1-q;
plot(p,ANSWER(1,:),'h',p,ANSWER(2,:),'.',p,ANSWER(3,:),'-')
title('Fig.10.2-Tie is hexagrams, Champ wins is dots, Challenger wins is solid')
xlabel('probability, p, of the Champ winning a game')
ylabel('probability')
grid
figure(1)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -