Fortran,LU方法可以';我不能让它正常工作
我正在做fortran作业,我必须编写LU方法,我写了一些行,但我被卡住了,因为它不能正常工作。 我的程序对数组1和2以及对U的处理很好,对L的处理也很好,但是3和4是错误的(L和U)。 你能帮我吗? 以下是节目:Fortran,LU方法可以';我不能让它正常工作,fortran,Fortran,我正在做fortran作业,我必须编写LU方法,我写了一些行,但我被卡住了,因为它不能正常工作。 我的程序对数组1和2以及对U的处理很好,对L的处理也很好,但是3和4是错误的(L和U)。 你能帮我吗? 以下是节目: program lu implicit none real*8 A(4,4),L(4,4),U(4,4) integer i,j,n,k open(unit=21,file='mat2.dat') open(unit=22,file='L.dat') open(unit=23,fil
program lu
implicit none
real*8 A(4,4),L(4,4),U(4,4)
integer i,j,n,k
open(unit=21,file='mat2.dat')
open(unit=22,file='L.dat')
open(unit=23,file='U.dat')
A=0.0d0
L=0.0d0
U=0.0d0
do i=1,4
read(21,*) (A(i,j),j=1,4) !read A matrix
end do
do i=1,4
L(i,1)=A(i,1) !creating array L(i,1)
U(1,i)=A(1,i)/L(1,1) !creating row U(1,i)
end do
do i=2,4
do j=2,4
if(i.eq.j) then
U(i,j)=1 !creating U diagonal=1, since U(1,1) is already created it can start from 2
end if
do n=1,j-1
if (i>=j) then
L(i,j)=A(i,j)-L(i,n)*U(n,j) !creating the L missing part, i think here is an error, but i can't find it
end if
end do
do n=1,i-1
if (i<j) then
U(i,j)=A(i,j)*1/L(i,i)-L(i,n)*U(n,j)*1/L(i,i) !creating the U missing part
end if
end do
end do
end do
do i=1,4
write(22,*) (L(i,j),j=1,4) !write to check if it's working fine
write(23,*) (U(i,j),j=1,4)
end do
end program
我和你应该看起来像这张照片
对不起,我的英语不好:(通常,一个人将L和U存储在一个唯一的矩阵中,并且,通常只修改用作输入和输出的初始矩阵 该算法相当简单;下面是一个没有枢轴搜索的变体:
SUBROUTINE matrix_lu(matrix)
! decomposing a matrix as a product of two triangular matrices (lower and upper)
! the diagonal belongs to the upper triangular matrix (the diagonal of the lower
! triangular matrix is assumed to be equal to the identity matrix)
! Take care : the diagonal of U is already inversed
DOUBLE PRECISION, INTENT(inout) :: matrix(:,:)
INTEGER :: n,i,k,l
n=SIZE(matrix,1)
DO k = 1, n
matrix(k, k) = 1.d0/matrix(k, k)
DO i = k+1, n
matrix(i, k) = matrix(i, k)*matrix(k, k)
ENDDO
DO l = k+1, n
DO i = k+1, n
matrix(i, l) = matrix(i, l)-matrix(i, k)*matrix(k, l)
END DO
END DO
END DO
END SUBROUTINE
所以我不明白你的算法中有几点:
- j上的第二个循环应为从i到n(或i+1到n):
- 循环中没有测试
- 全局复杂度(乘法数)为n^3/3。您的算法为n^3
SUBROUTINE matrix_lu(matrix)
! decomposing a matrix as a product of two triangular matrices (lower and upper)
! the diagonal belongs to the upper triangular matrix (the diagonal of the lower
! triangular matrix is assumed to be equal to the identity matrix)
! Take care : the diagonal of U is already inversed
DOUBLE PRECISION, INTENT(inout) :: matrix(:,:)
INTEGER :: n,i,k,l
n=SIZE(matrix,1)
DO k = 1, n
matrix(k, k) = 1.d0/matrix(k, k)
DO i = k+1, n
matrix(i, k) = matrix(i, k)*matrix(k, k)
ENDDO
DO l = k+1, n
DO i = k+1, n
matrix(i, l) = matrix(i, l)-matrix(i, k)*matrix(k, l)
END DO
END DO
END DO
END SUBROUTINE