Monte carlo integral evaluation

Click For Summary

Homework Help Overview

The discussion revolves around evaluating a Gaussian integral using a Monte Carlo method, specifically focusing on the computation of the expected value of a squared variable from a normal distribution. The original poster presents a MATLAB code snippet intended to implement this approach but encounters errors during execution.

Discussion Character

  • Exploratory, Problem interpretation, Assumption checking

Approaches and Questions Raised

  • The original poster attempts to use a Monte Carlo simulation to estimate the integral by generating random samples from a normal distribution. Participants inquire about the specific error messages and the programming language used, as well as the definition of variables in the code.

Discussion Status

Participants are actively engaging with the original poster's code, seeking clarification on error messages and variable definitions. There is a focus on identifying potential issues in the code, particularly regarding variable naming and scope.

Contextual Notes

The original poster has defined a variable N for the number of samples but has not defined the variable n used in the for loop, which may contribute to the errors encountered. The discussion highlights the importance of consistent variable usage in programming.

randonmind
Messages
2
Reaction score
0
Hello
I need to evaluate Gaussian integral
K=1/(sqrt(2pi) int(-infinity +infinity) x^2 exp(-x^2/2) dx
by using
K=E[X^2] ∼Kn=1/N sum(x^2)
where x follows a normal distribution.
So far I have written this:
N=1000;
for i=1:n
x1(i+1)=x(i)+1/N*random('normal',0,1).^2
end
x1
but I am getting error messages.

Can anyone share their wisdom?
Thanks
Andreas
 
Physics news on Phys.org
What error messages are you gettting and what programming language are you using?
 
it is in Matlab:
Error in ==> MCexam3 at 13
x1(i+1)=x(i)+1/N*random('normal',0,1).^2
 
are you mixing up your big N and little n ? in the for loop you use n, but you haven't defined it anywhere.
 

Similar threads

Replies
1
Views
1K
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K