Calculating an Integral by dartboard integration
Informazioni
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
Mostra commenti meno recenti
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 Commento
Weird Rando
il 7 Mag 2016
Modificato: Weird Rando
il 7 Mag 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.
Risposte (0)
Questa domanda è chiusa.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!