mardi 21 février 2017

creating a complex random matrix in fortran

Hi I am trying to create a 3x3 complex random matrix in Fortran. I want the matrix to be random by distributing it with random numbers drawn from a Gaussian distribution with mean zero. However, I want each matrix element to be a different random complex number. I found another question similar on here but it's for a real matrix, and I just am not able to apply it to my code. The link to that question is here Fortran: random matrix intitialisation: A portion of my code illustrating the problem is below.

integer, parameter :: n = 10
integer:: k,l 
real:: mu,sigma,pi,x1,x2,y1,y2 
complex,dimension(3,3) :: RM


pi=4.0*atan(1.0)
sigma = 1.0
mean  = 0.0

call random_seed
call random_number(x1)
call random_number(x2)

y1=sqrt(-2*log(x1))*cos(2*pi*x2) !generate random numbers via Box Muller Transform
y2=sqrt(-2*log(x1))*sin(2*pi*x2)

y1=y1*sigma+mu !specify mean and variance of random number
y2=y2*sigma+mu

do k=1,3
do l=1,3
         RM(k,l) = cmplx(y1,y2)
end do
end do

do k=1,3
  print*, (RM(k,l), l=1,3) 
end do

My expected result is : a 3x3 complex matrix with each matrix element distributed with a different random complex number. However this is not what I obtain when I print out my result. Instead, my output is one complex matrix with the same random number generated for each matrix element. How can I obtain my expected result and fix the code below? Thanks!




Aucun commentaire:

Enregistrer un commentaire