1 / 19

モンテカルロ シミュレーション

P129. モンテカルロ シミュレーション. 乱数を使ったシミュレーション. モンテ・カルロ. モナコ 公国 の北部を占める地区。 南の首都モナコ市とは港を挟んで反対側(北)に位置する。 1856 年シャルル 3 世が公国の財源確保のため 賭博 (とばく)場( カジノ )の開設を許可、 1861 年に開設して以来、 カンヌ 、 ニース と並び、地中海の コート・ダジュール 有数の 観光 ・保養地、海水浴場として発達した。. モンテ・カルロ. C言語での乱数発生. rand( ) 0 ~ RAND_MAX  までの ランダムな整数値が発生する。 (32767)

keira
Download Presentation

モンテカルロ シミュレーション

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. P129 モンテカルロシミュレーション 乱数を使ったシミュレーション

  2. モンテ・カルロ モナコ公国の北部を占める地区。 南の首都モナコ市とは港を挟んで反対側(北)に位置する。 1856年シャルル3世が公国の財源確保のため賭博(とばく)場(カジノ)の開設を許可、1861年に開設して以来、カンヌ、ニースと並び、地中海のコート・ダジュール有数の観光・保養地、海水浴場として発達した。

  3. モンテ・カルロ

  4. C言語での乱数発生 rand() 0 ~ RAND_MAX までの ランダムな整数値が発生する。 (32767) #include<stdlib.h> を使用

  5. プログラム例 #include <stdio.h> #include <stdlib.h> int main() { int irandx, i, n=4; for(i=0;i<n;i++){ irandx = rand(); printf("%d\n",irandx); } return 0; }

  6. 0~1の乱数の発生 0~1の実数を与えるには、 rand( )を最大値RAND_MAXで割る。 例: randx = (double) rand()/(double) RAND_MAX;

  7. プログラム例 #include <stdio.h> #include <stdlib.h> int main() { int i, n=4; double randx; for(i=0;i<n;i++){ randx = (double)rand()/(double)RAND_MAX; printf("%lf\n",randx); } return 0; }

  8. 円の面積 0.5 - 0.5 0 0.5 円の面積=(円内の個数)/(全部の個数) - 0.5

  9. 円の面積 0.5 (x1, x2) - 0.5 0 0.5 円内の条件 ⇒ (x12+ x22 )<0.25 - 0.5

  10. 球の体積 (x1, x2, x3) 球内の条件 ⇒ (x12+ x22 + x32 )<0.25

  11. 多次元空間の球内の条件 三次元 x12 + x22 + x32 < 0.25 四次元 x12 + x22 + x32 + x42 < 0.25 五次元 x12 + x22 + x32 + x42 + x52 < 0.25 ただし,‐0.5 < x1, x2, x3, x4, x5 <0.5

  12. 多次元空間の球の体積 m[j]=0 m[j]=m[j]+1 乱数で,x1, x2, x3, …を生成 x12 + x22 + x32<0.25 Yes No 繰り返し数:N 体積=m[j]/N

  13. #include <stdio.h> #include <stdlib.h> #include <math.h> #define N 10000 #define DIM 5 int main( ) { double x[DIM], r[DIM]; int n, j; double rr; int m[DIM+1]; for(j=0;j<DIM;j++) m[j]=0; 乱数の点数 5次元まで 球内の個数を カウントする変数

  14. printf(" 回数  円面積  球体積  四次元  五次元\n\n"); printf(" 回数  円面積  球体積  四次元  五次元\n\n"); for(n=1;n<=N;n++){ for(j=0;j<DIM;j++){ x[j]=(double)rand()/32768.; r[j]=0.0; } rr=0.0; for(j=0;j<DIM;j++){ rr+=(x[j]-0.5)*(x[j]-0.5); r[j]=rr; } for(j=0;j<DIM;j++) if(r[j]<0.25) m[j]++; if((n%1000)==0){ printf("%6d ",n); for(j=1;j<DIM;j++) printf("%9.4lf ",(double)m[j]/(double)n); printf("\n"); } } return 0; } 乱数発生 0~1 各次元ごと 距離を計算 各次元ごと 球内点を加算 全点数で割って 球体積を計算」

  15. 乱数の初期値を変更する srand((unsigned)time(NULL)); をrand()の前に入れる。 #include<time.h>; を使用

  16. プログラム例 #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int i, n=4; double randx; srand((unsigned)time(NULL)); for(i=0;i<n;i++){ randx = (double)rand()/(double)RAND_MAX; printf("%lf\n",randx); } return 0; }

  17. 例題 待つ時間:0~15分 0 15 30 45 15分間隔でくるバスの待つ時間の平均?

  18. 15分間隔のバスを待つ時間 #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int i, n=100; double randx=0.0; srand((unsigned)time(NULL)); for(i=0;i<n;i++) randx += 15* (double)rand()/(double)RAND_MAX; randx=randx/(double)n; printf(“%lf\n",randx); return 0; }

  19. 15分間隔のバス • 10分以上待つ確率を求めるプログラムを書け。 • 学部コード;25 • 時間割コード;63270 • 曜日・時限:木2

More Related