文档详情

实验蒙特卡洛模拟.doc

发布:2017-03-27约字共4页下载文档
文本预览下载声明
Using Monte Carlo simulation, write a Matlab program to calculate an approximation to ( by considering the number of random points selected inside the quarter circle where the quarter circle is taken to be inside the square n=50000; counter=0; for k=1:n x=rand(1); y=rand(1); if x.^2+y.^2=1 counter=counter+1; end end PI=4.*counter./n 运行结果: PI = 3.1420 Using Monte Carlo simulation, write a Matlab program to calculate the volume of an ellipsoid(椭球体) n=50000; counter=0; for k=1:n x=rand(1)*6; y=rand(1)*8; z=rand(1)*12; if (x.^2)/2+(y.^2)/4+(z.^2)/8=16 counter=counter+1; end end volume=8*(counter/n)*6*8*12 运行结果: volume = 2.1469e+003 Use Monte Carlo simulation to simulate the rolls of an unfair die. Assume the die is biased according to the following empirical distribution: Roll Value Probability 1 0.1 2 0.1 3 0.2 4 0.3 5 0.2 6 0.1 n=50000; counter1=0; counter2=0; counter3=0; counter4=0; counter5=0; counter6=0; for k=1:n x=rand(1)*10; if x=1 counter1=counter1+1; elseif x=2 counter2=counter2+1; elseif x=4 counter3=counter3+1; elseif x=7 counter4=counter4+1; elseif x=9 counter5=counter5+1; elseif x=10 counter6=counter6+1; end end P1=counter1/n P2=counter2/n P3=counter3/n P4=counter4/n P5=counter5/n P6=counter6/n 运行结果: P1 = 0.0993 P2 = 0.1009 P3 = 0.1988 P4 = 0.3015 P5 = 0.1997 P6 = 0.0997 You arrive at the beach for a seven-day vacation and are dismayed to learn that the local weather prediction is predicting a 50% chance of rain every day. Using Monte Carlo simulation, predict the chance that it rains at least three consecutive(接连不断的,连续而来的) days in your vacation. i=0;c=0; n=50000; counter=0; for k=1:n for t=1:7 if rand(1)=0.5; c=c+1; else c=0; end if c==3; i=1;
显示全部
相似文档