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 commentaire

Weird Rando
Weird Rando le 7 Mai 2016
Modifié(e) : Weird Rando le 7 Mai 2016
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.

Réponses (0)

Cette question est clôturée.

Question posée :

le 7 Mai 2016

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by