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

조회 수: 6 (최근 30일)
健
2022년 10월 29일
댓글: 2022년 10월 30일
ビュフォンの針という、円周率をシュミレーションによって求める問題を研究している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の値を、実行するたびに今までの値累積平均をとって保存したいと思っています。
方法を教えていただけるとありがたいです。

채택된 답변

Atsushi Ueno
Atsushi Ueno 2022년 10월 30일
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
Atsushi Ueno
Atsushi Ueno 2022년 10월 30일
averagepiを求める為にpiEstAryは不要である事に注意ください。
健
2022년 10월 30일
そうですね!ありがとうございました。

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 記述統計と可視化에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!