随机模拟实验
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
教师:龚敏庆
学生:吴欢、谢德悠、邓春霞、李丽、杨思文、汪德萍、贺阳、赵泽海Example 3a:
>> s=0;
for i=1:500000
a=rand;
b=rand;
if a^2+b^2<=1
s=s+1;
end
end
pi=4*s/500000
pi =
:
x0=3;
xn=zeros(1,11);
x(1)=x0;
for i=1:10
x(i+1)=mod(5*x(i)+7,200);
end
x
x =
3 22 117 192 167 42 17 92 67 142 117
:
for i=1:100
x(i)=rand;
end
g=exp(exp(x));
theta=mean(g)
theta =
:
>> N=[];
for i=1:100
V=cumsum(rand(1,100));
N=[N 1+sum(V<=1)];
end
mean(N)
ans =
N=[];
for i=1:10000
V=cumsum(rand(1,10000));
N=[N,1+sum(V<=1)];
end
mean(N)
ans =
Example 4b
P=1:20;
k=20;
while k>1
U=rand;
i=fix(k*U)+1;
m=P(k);P(k)=P(i);P(i)=m;
k=k-1;
end
>> P
P =
9 4 10 1 5 19 11 17 18 15 6 12 7 3 8 16 13 2 20 14
:
>> X=1+(rand(1,100)>1/3);
tabulate(X)
Value Count Percent
1 34 %
2 66 %
>> X=1+(rand(1,1000)>1/3); tabulate(X)
Value Count Percent
1 337 %
2 66
3 %
>> X=1+(rand(1,10000)>1/3); tabulate(X)
Value Count Percent
1 3288 %
2 6712 %
>> X=1+(rand(1,100000)>1/3); tabulate(X)
Value Count Percent
1 33463 %
2 66537 %
>> s=0;
>> for i=1:1000
u=rand;
if u<=1/3
s=s+1;
end
end
>> s
s =
332
>> s/1000
ans =
:
>> X=randn(1,1000);
>> Y=abs(X);
>> mean(Y)
ans =
X=randn(1,10000);
Y=abs(X);
mean(Y)
ans =
:
u=rand;
if u<
x=2*floor(u/+5;
else x=2*floor(/+6; end
u
x
u =
x =
6
>> u=rand;
if u<
x=2*floor(u/+5;
else x=2*floor(/+6; end
u
x
u =
x =
13
:
u=rand;
if u<
x=floor(u/+1; elseif u<
x=3*(floor(/+1)+3; elseif u<
x=3*(floor(/+1)+4; else
x=8;
end
u
x
u =
x =
4
u=rand;
if u<
x=floor(u/+1; elseif u<
x=3*(floor(/+1)+3; elseif u<
x=3*(floor(/+1)+4; else
x=8;
end
u
x
u =
x =
8
u=rand;
if u<
x=floor(u/+1; elseif u<
x=3*(floor(/+1)+3; elseif u<
x=3*(floor(/+1)+4; else
x=8;
end
u
x
u =
x =
7
u=rand;
if u<
x=floor(u/+1;
elseif u<
x=3*(floor(/+1)+3;
elseif u<
x=3*(floor(/+1)+4;
else
x=8;
end
u
x
u =
x =
9
:
q1=1/2;
q2=2/3;
u=rand(1,100);
if u<1/2
X=floor(log(u)/log(q1))+1
else X=floor(log(u)/log(q2))+1; end
u,X
u =
Columns 1 through 12