I want to write a matlab script that uses simulation to evaluate the following integral using 1000 iterations:
This is my code:
The output is:Code:n=1000; total=0; for i=1:n x= rand()*2+2; y=x^2; total=total+y; end expectation=total/n; integral=expectation*4
But this answer is not even close!! Why is that? Can anyone show the problem with my code?