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

📄 randomsum.m

📁 一些常被用于教学或者参考的概率论的实例的源代码
💻 M
字号:
%randomsum.m/created by PJNahin for "Duelling Idiots"(6/3/98)
%This m-file forms sums of terms, with each term uniformly and
%independently distributed from 0 to 1. New terms are added until
%the sum is at least equal to 1. Then, the number of terms
%required is saved and a new sum is started.
%
%
rand('state',100*sum(clock));     %new seed for generator
d=zeros(1,20);                    %initialize vector of number of
                                  %in a sum, i.e., d(i)=# of sums
                                  %that required i terms to reach
                                  %or exceed 1.
k=1;                              %get ready to form first sum;
while k<=100000                   %have we formed last sum?
   n=0;                           %no, get ready to form new sum by
   summation=0;                   %setting both n (current number of
                                  %terms in sum), and summation to zero;
      while summation<1           %has sum reached 1?
         summation=summation+rand;%no, add another term;
         n=n+1;                   %up-date number of terms;
      end
      d(n)=d(n)+1;                %yes, sum has reached threshold,
      k=k+1;                      %and so up-date number-of-terms
   end                            %vector, and the number of sums
                                  %formed so far;
e=0;                              %start to calculated the expected
for n=1:20                        %value of the number of terms in a sum
   e=e+n*d(n);
end
e/100000                          %print simulation estimate for E(N);

         
                                     

⌨️ 快捷键说明

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