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!