Reputation: 413
I have a portion of my code below which solves a stochastic heat equation in 1D with periodic boundary conditions. The stochastic term is Gaussian white noise.
My question is, am I implementing the noise correctly?
Gaussian noise is defined as having mean zero, and second moment which tells us that values at any pair of times are identically distributed and statistically independent.
!I first define arrays for the uniform and gaussian random numbers
real, dimension (-1:n) :: u,v,x1,x2,y1,y2
real :: k=0.005,h=0.1,R !time and position step, respectively
R=k/h**2.
!generate Uniform, then Gaussian Random numbers for White Gaussian Noise
call random_seed
call random_number(x1)
call random_number(x2)
y1=sqrt(-2*log(x1))*cos(2*pi*x2)
y2=sqrt(-2*log(x1))*sin(2*pi*x2)
y1=y1*sigma+mu
y2=y2*sigma+mu
do i=0,n-1
v(0)=v(n)
v(-1)=v(n-1)
v(i) = (1-2.0*R)*u(i)+R*(u(i+1)+u(i-1))+k*y1(i) !discretized stochastic heat eqn
end do
Is this the proper way to add Gaussian noise into my code? I only use the Gaussian random number y1, I do not need y2. Is this correct? Thanks!
Upvotes: 1
Views: 434
Reputation: 2605
You appear to be using the Box-Muller transform to obtain normally distributed samples from uniform samples. Your implementation appears to be correct, so y1 and y2 are indeed independent random variables with a Gaussian distribution.
Upvotes: 2