Calculating an Integral by dartboard integration
Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen
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 Kommentar
Weird Rando
am 7 Mai 2016
Bearbeitet: Weird Rando
am 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.
Antworten (0)
Diese Frage ist geschlossen.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!