シミュレーション MEMO

● F-BASICプログラムの練習 

1.級数の和

N=100:S=0
for I=1 to N:S=S+I:next
print "N=";N;"....";S
stop
end

2. 自然数の2乗の和,3乗の和を求めるプログラムを作成し,実行し「和の公式」を確かめる。

N=50:S1=0:S2=0:S3=0
for I=1 to N:S1=S1+I:S2=S2+I^2:S3=S3+I^3:next
print "n=";N
print using "sum k=###### N(N+1)/2=######";S1,N*(N+1)/2
print using "sum k^2 ###### N(N+1)(2N+1)/6=######";S2,N*(N+1)*(2*N+1)/6
print using "sum k^3 ####### N^2(N+1)^2/4=#######";S3,N^2*(N+1)^2/4
stop:end

● F-BASICプログラムの練習 2

シミュレーションの基礎
1.乱数の発生:randomize と rnd(1) (さいころの目 10個)


input "RN=";rn:randomize rn
for i=1 to 10:x=int(rnd(1)*6+1):print x;:next
stop:end

2.酔歩の問題:N歩無秩序に歩いたら,出発地点からいくらの距離に達するか?

dim dx(4),dy(4)
for i=1 to 4:read dx(i),dy(i):next
input "RN=";rn:randomize rn:input "N=";n
color 0,15:cls:x0=300:y0=200:x=0:y=0:l=5
circle(x0,y0),3,5
for i=1 to n
ir=int(rnd(1)*4+1):x1=x+dx(ir):y1=y+dy(ir)
line(x0+x*l,y0-y*l)-(x0+x1*l,y0-y1*l),,0
x=x1:y=y1
next
circle(x0+x*l,y0-y*l),3,9
stop
data 1,0, 0,1, -1,0, 0,-1
end

上の図はN=500での実行例。出発点:●,終点:★

3.上のプログラムを参考にN歩後の位置は平均として,出発点から√N歩の距離にいることを確かめる。


dim dx(4),dy(4)
for i=1 to 4:read dx(i),dy(i):next
input "RN=";rn:randomize rn:input "N=";n
input "number of tries";m:color 0,15:cls:r2=0
for k=1 to m
x=0:y=0
for i=1 to n
ir=int(rnd(1)*4+1):x=x+dx(ir):y=y+dy(ir)
next
r2=r2+x^2+y^2
next
print "N=";n;" M=";m;" R=";sqr(r2/m):stop
data 1,0, 0,1, -1,0, 0,-1
end


● 1次元原子列と惑星の運動  memo

 逆関数法による乱数の発生  memo
 
 拡散と酔歩  
memo

 マックスウエルの速度分布則
解説 → maxwell.pdf   FBASICプログラム → こちら

     実行ファイル


ホームへ