《用蒙特卡洛计算积分》由会员分享,可在线阅读,更多相关《用蒙特卡洛计算积分(10页珍藏版)》请在金锄头文库上搜索。
1、用蒙特卡洛计算积分1 用蒙特卡洛方法估计积分 , 和 的值,并将估计值20sinxd2-0xe221xyxyed与真值进行比较。1. 的 真值为 120sinxdclearclctrue=1;for t=1:1:50n=1000; a=(pi)/2*rand(1,n);for i=1:1:nb(i)=(pi)/2*a(i)*sin(a(i);endc(t)=mean(b);endcmean(c-true).*(c-true)结论 当 N 为 200 时 计算的值分别为c =Columns 1 through 81.0777 0.8619 1.0677 1.0151 1.0311 1.0291
2、0.9675 0.9751Columns 9 through 160.9502 0.9672 0.9271 0.9416 0.9899 1.0854 1.0175 0.9739Columns 17 through 240.9612 1.0555 0.9526 1.0375 1.0233 1.0361 0.9694 0.9995Columns 25 through 321.0538 0.9941 1.0114 1.0576 1.1029 0.9244 1.0831 0.9029 Columns 33 through 401.0329 0.8919 1.0362 0.9808 0.9724 1.0
3、233 1.0386 0.9858Columns 41 through 480.9704 0.9825 0.9741 1.1060 1.0521 0.8751 1.0642 0.9985Columns 49 through 500.9915 0.9792ans =0.0032当 N 为 500 时 计算的值分别为c =Columns 1 through 80.9614 0.9748 0.9687 0.9646 1.0441 0.9816 0.8864 1.0066Columns 9 through 160.9673 0.9897 1.0502 0.9713 1.0277 1.0071 1.02
4、52 1.0193Columns 17 through 241.0236 0.9899 0.9883 0.9983 1.0300 0.9259 1.0385 0.9855Columns 25 through 321.0057 1.0056 1.0918 1.0327 0.9601 1.0232 0.9806 1.0470Columns 33 through 401.0505 0.9945 1.0287 1.0317 1.0248 1.0316 1.0248 1.0013Columns 41 through 48 0.9863 0.9696 0.9993 1.0207 1.0333 1.0618
5、 1.0705 1.0144Columns 49 through 500.9454 1.0030ans =0.0014当 N 为 1000 时 计算的值分别为c =Columns 1 through 81.0036 0.9988 0.9848 0.9598 0.9653 0.9595 1.0015 1.0034Columns 9 through 161.0378 1.0291 1.0006 1.0548 1.0178 1.0227 1.0087 0.9922Columns 17 through 241.0389 0.9956 0.9669 1.0135 1.0392 1.0228 1.0088
6、 1.0390Columns 25 through 320.9871 1.0236 1.0245 1.0038 1.0065 0.9701 0.9918 1.0066Columns 33 through 400.9790 0.9985 1.0474 0.9783 0.9997 1.0450 0.9524 1.0147Columns 41 through 481.0001 0.9929 1.0384 0.9813 0.9696 1.0088 1.0085 0.9837Columns 49 through 500.9938 1.0066ans =6.1578e-004再用莱姆大为 1 的指数分布估
7、计clearclctrue=1;for t=1:1:50n=1000;b=exprnd(1,1,n);for i=1:1:nif(b(i)0&b(i)0&b(i)1)c(i)=exp(b(i)*b(i);elsec(i)=0;endenda(t)=mean(c);endavar(a)a =Columns 1 through 81.4675 1.4531 1.4591 1.4754 1.4712 1.4765 1.4461 1.4575Columns 9 through 161.4629 1.4569 1.4541 1.4663 1.4605 1.4498 1.4505 1.4620Column
8、s 17 through 241.4675 1.4727 1.4772 1.4662 1.4650 1.4500 1.4546 1.4426Columns 25 through 321.4635 1.4834 1.4513 1.4525 1.4911 1.4488 1.4798 1.4437Columns 33 through 401.4525 1.4724 1.4457 1.4622 1.4588 1.4498 1.4504 1.4606Columns 41 through 481.4631 1.4772 1.4689 1.4612 1.4699 1.4574 1.4715 1.4678 C
9、olumns 49 through 501.4600 1.4531ans =1.2042e-004ans =1.4629241xydxyfor t=1:1:50n=1000;r=sqrt(rand(1,n); seta=2.*pi.*rand(1,n); x=r.*cos(seta); y=r.*sin(seta); for i=1:1:nc(i)=pi/sqrt(1+x(i)*x(i)*x(i)*x(i)+y(i)*y(i)*y(i)*y(i);enda(t)=mean(c);endavar(a)a =Columns 1 through 82.8452 2.8304 2.8501 2.840
10、0 2.8424 2.8435 2.8325 2.8475Columns 9 through 162.8390 2.8417 2.8350 2.8444 2.8417 2.8495 2.8256 2.8448 Columns 17 through 242.8589 2.8415 2.8364 2.8588 2.8487 2.8518 2.8426 2.8409Columns 25 through 322.8440 2.8295 2.8550 2.8470 2.8377 2.8476 2.8554 2.8342Columns 33 through 402.8381 2.8545 2.8395 2.8437 2.8498 2.8475 2.8319 2.8399Columns 41 through 482.8492 2.8267 2.8614 2.8413 2.8381 2.8511 2.8537 2.8430Columns 49 through 502.8410 2.8543ans =6.9356e-005ans =2.8447