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

📄 paths.m

📁 一些常被用于教学或者参考的概率论的实例的源代码
💻 M
字号:
%paths.m/created by PJNahin for "Duelling Idiots"(6/10/98)
%This m-file estimates the distribution and density functions
%of the random variable L denoting random path lengths
%across a unit square.
%
%
rand('state',100*sum(clock))    %set new seed for generator;
length=zeros(1,142);            %length is vector of path lengths,
for n=1:100000                  %in bins of width 0.01;
   x=rand;                      %get a random entry point;
   angle=pi*rand;               %get a random angle;
   A1=atan(1/(1-x));
   if angle<A1                  %pass through right vertical edge? 
      L=(1-x)/cos(angle);       %yes;
   elseif angle>A1 & angle<pi-atan(1/x)    %pass through top?
      L=1/sin(angle);                      %yes;
   else
      L=-x/cos(angle);          %passes through left vertical edge
   end
   index=round(100*L+.5);          %round path length to nearest .01
                                   %(adding the '.5' prevents getting
                                   %a zero index);
   length(index)=length(index)+1;  %up-date length vector;
end
x=.01:.01:1.42;
subplot(1,2,1)
plot(x,length/1000)
grid
xlabel('length')
ylabel('magnitude')
title('Density of L from 100,000 Paths')
subplot(1,2,2)
dist(1)=length(1);               %dist is distribution vector
for index=2:142
   dist(index)=dist(index-1)+length(index);
end
plot(x,dist/100000)
grid
xlabel('length')
ylabel('probability')
title('Distribution of L from 100,000 Paths')
figure(1)

⌨️ 快捷键说明

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