Calculating an Integral by dartboard integration
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
I'm trying to calculate the integral

and my code is the following
Y=exp(-(pi/2)^2);
A=(pi/2)*Y;
N=100000;
s=0;
for i= 1:N
x=(pi/2).*rand(1,1);
y=Y.*rand(1,1);
if y<=sin(x).*exp(-(x.^2));
s=s+1;
end;
end;
I=(A*s)/N
this gives me 0.12 as the result which is wrong it should be about 0.40 I don't know what is wrong with this code thank yo for your help
1 件のコメント
Weird Rando
2016 年 5 月 7 日
編集済み: Weird Rando
2016 年 5 月 7 日
Sorry I don't know anything about dartboard. But here is what's wrong.
Lets say all the sample fits the if condition (y<=sin(x).*exp(-(x.^2))).
thus your answer is:
I = (A*10000)/10000
A = (pi/2)*Y
Y = exp(-(pi/2)^2)
Hence,
Y = (pi/2)*exp(-(pi/2)^2)
Y = 0.1332
What you should be doing is probably adding all the variable y together and then take the mean of y.
回答 (0 件)
この質問は閉じられています。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!