累積平均の求め方を知りたいです

16 views (last 30 days)
健
on 29 Oct 2022
Commented: on 30 Oct 2022
ビュフォンの針という、円周率をシュミレーションによって求める問題を研究しているmatlab初心者の学生です。
得られた値の累積平均が実際の円周率の値にどのように収束するか調べたいのですが、どのように累積平均を取ればいいのかわかりません。
教えていただきたいです。
N=1000;%針の数
L=0.20;%針の長さ
xb=L+rand(1,N)*(1-2*L);%針の始点のx座標
yb=L*rand(1,N)*(1-2*L);%針の始点のy座標
angs=rand(1,N)*360;%針の傾き
xe=xb+L*cosd(angs);
ye=yb+L*sind(angs);
ax=axes;
plot(ax,[xb;xe],[yb;ye]);%針を表示
axis square
hold on
glines=0:L:1;%平行線を表示
for i =1:length(glines)
xline(ax,glines(i));
end
n=sum(floor(xb/L)~=floor(xe/L));%平行線に交わった針の数
n = 627
piEstimate=2*N/n%円周率の値を求める
piEstimate = 3.1898
毎回変数を変え、このスクリプトで得られたpiEstimateの値を、実行するたびに今までの値累積平均をとって保存したいと思っています。
方法を教えていただけるとありがたいです。

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 30 Oct 2022
piEstAry = []; % 最初に空ベクトルとして初期化しておく
for i = 1:5
piEstAry = [piEstAry, i] % 過去の演算値をベクトルに追記していく
piEstimate = mean(piEstAry) % 平均値を得る
end
piEstAry = 1
piEstimate = 1
piEstAry = 1×2
1 2
piEstimate = 1.5000
piEstAry = 1×3
1 2 3
piEstimate = 2
piEstAry = 1×4
1 2 3 4
piEstimate = 2.5000
piEstAry = 1×5
1 2 3 4 5
piEstimate = 3
  6 Comments
Atsushi Ueno
Atsushi Ueno on 30 Oct 2022
averagepiを求める為にpiEstAryは不要である事に注意ください。
健
on 30 Oct 2022
そうですね!ありがとうございました。

Sign in to comment.

More Answers (0)

Categories

Find more on グラフィックス パフォーマンス in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!