主题:fortran与matlab得到不同结果
我做了一个程序,求矩阵特征值,但是发现矩阵的值跟matlab得到的矩阵值不同,不知道哪个地方出问题了,各位大侠帮我看下吧:
program main
use IMSL
! use linear_operators
implicit none
real, parameter::pi = 3.14159265
complex, parameter:: fi = (0.0, 1.0)
real t,f,w
complex, allocatable:: h0(:, :)
real, allocatable:: eigenvalue(:) !特征值的矩阵
integer :: i, n
n = 5
allocate(h0(4 * n, 4 * n))
allocate(eigenvalue(4 * n))
w = 0.0
t = 1.0
f = 0.25
call hmatr(h0, n)
eigenvalue=eig(h0)
do i=1,4*n
write(*,"('eigenvalue=',f10.7)")eigenvalue(i)
! write(*,"('eigenvector=['3(f5.2'')']')")eigenvector(:,i)
end do
stop
contains
subroutine hmatr(h0,n)
integer n
complex h0(:, :)
integer:: j, n0
complex:: c(4,4),a(4,4),b(4,4)
real:: w
h0 = 0.0
a = 0.0
a(1, 4) = t
b = transpose(a)
c = 0.0
do n0=1,2*n-1,2
c(2, 1) = t*exp(-fi*(-pi+2*pi/3*f*(1.5*n0+1/4)))
c(1, 2) = conjg( c(2, 1) ) !t*exp(fi*(-pi+2*pi/3*f*(1.5*n0+1/4)))
c(2, 2) = w
c(2, 3) = t
c(3, 2) = t
c(3, 3) = w
c(3, 4) = t*exp(fi*(-pi+2*pi/3*f*(1.5*(n0+1)+1/4)))
c(4, 3) = conjg( c(3, 4) ) !t*exp(-fi*(-pi+2*pi/3*f*(1.5*(n0+1)+1/4)))
c(4, 4) = w
j=2*n0-1
do j=1,4*n-3,4
h0(j:j+3,j:j+3)=c
if(j==1) then
h0(j:j+3,j+4:j+7)=b;
h0(j:j+3,4*n-3:4*n)=a;
else if(j>=5.and.j<4*n-3) then
h0(j:j+3,j+4:j+7)=b;
h0(j:j+3,j-4:j-1)=a;
else
h0(4*n-3:4*n,1:4)=b;
h0(j:j+3,j-4:j-1)=a;
end if
end do
end do
end subroutine hmatr
end program main
矩阵h0虽然得到了特征值,但是这个矩阵可能违背了我的本意了,我没找到哪地方出问题了。从程序看到,每变化一个n0,都会有不同的c矩阵,但是如果把h0矩阵以文件的形式写下来发现在h0矩阵里出现好几个这个数:
(-0.7071066,0.7071069)
怎么会出现很多呢?我想可能n0就取了一个值,其他值没取到吧?因为
c(3, 4) = t*exp(fi*(-pi+2*pi/3*f*(1.5*(n0+1)+1/4)))
随着n0值不同,会变化的。百思不得其解啊。不会调试程序,埃
program main
use IMSL
! use linear_operators
implicit none
real, parameter::pi = 3.14159265
complex, parameter:: fi = (0.0, 1.0)
real t,f,w
complex, allocatable:: h0(:, :)
real, allocatable:: eigenvalue(:) !特征值的矩阵
integer :: i, n
n = 5
allocate(h0(4 * n, 4 * n))
allocate(eigenvalue(4 * n))
w = 0.0
t = 1.0
f = 0.25
call hmatr(h0, n)
eigenvalue=eig(h0)
do i=1,4*n
write(*,"('eigenvalue=',f10.7)")eigenvalue(i)
! write(*,"('eigenvector=['3(f5.2'')']')")eigenvector(:,i)
end do
stop
contains
subroutine hmatr(h0,n)
integer n
complex h0(:, :)
integer:: j, n0
complex:: c(4,4),a(4,4),b(4,4)
real:: w
h0 = 0.0
a = 0.0
a(1, 4) = t
b = transpose(a)
c = 0.0
do n0=1,2*n-1,2
c(2, 1) = t*exp(-fi*(-pi+2*pi/3*f*(1.5*n0+1/4)))
c(1, 2) = conjg( c(2, 1) ) !t*exp(fi*(-pi+2*pi/3*f*(1.5*n0+1/4)))
c(2, 2) = w
c(2, 3) = t
c(3, 2) = t
c(3, 3) = w
c(3, 4) = t*exp(fi*(-pi+2*pi/3*f*(1.5*(n0+1)+1/4)))
c(4, 3) = conjg( c(3, 4) ) !t*exp(-fi*(-pi+2*pi/3*f*(1.5*(n0+1)+1/4)))
c(4, 4) = w
j=2*n0-1
do j=1,4*n-3,4
h0(j:j+3,j:j+3)=c
if(j==1) then
h0(j:j+3,j+4:j+7)=b;
h0(j:j+3,4*n-3:4*n)=a;
else if(j>=5.and.j<4*n-3) then
h0(j:j+3,j+4:j+7)=b;
h0(j:j+3,j-4:j-1)=a;
else
h0(4*n-3:4*n,1:4)=b;
h0(j:j+3,j-4:j-1)=a;
end if
end do
end do
end subroutine hmatr
end program main
矩阵h0虽然得到了特征值,但是这个矩阵可能违背了我的本意了,我没找到哪地方出问题了。从程序看到,每变化一个n0,都会有不同的c矩阵,但是如果把h0矩阵以文件的形式写下来发现在h0矩阵里出现好几个这个数:
(-0.7071066,0.7071069)
怎么会出现很多呢?我想可能n0就取了一个值,其他值没取到吧?因为
c(3, 4) = t*exp(fi*(-pi+2*pi/3*f*(1.5*(n0+1)+1/4)))
随着n0值不同,会变化的。百思不得其解啊。不会调试程序,埃