2个回答
展开全部
我运行通过了~~
program main
implicit none
integer :: N = 3
real*8 :: A(3,3) = (/ 2, 3, 1, 1, 1, 2, 1, 2, 2 /)
real*8 :: B(3) = (/ 4, 6, 5/), X(3)
call LinearGauss (A, B, X, N)
write(*,*) X
end
!-------------------------------------------------
! 行尺度高斯消元法
!-------------------------------------------------
subroutine LinearGauss (A, B, X, N)
implicit none
integer :: N
real*8 :: A(N, N), B(N), X(N)
real*8 :: z
integer,allocatable :: p(:), s(:)
integer :: i, j, k, sl
allocate (p(N))
allocate (s(N))
do i=1, N
p(i) = i
s(i) = MaxVal ( Abs(A(i, :)) )
end do
do k=1, N-1
j = k
do i=k+1, N
if ( Abs(A(p(j), k)) / s(p(j)) < Abs(A(p(i), k)) / s(p(i)) ) then
j = i
end if
end do
sl = p(k)
p(k) = p(j)
p(j) = sl
do i=k+1, N
z = A(p(i), k) / A(p(k), k)
A(p(i), k) = z
do j=k+1, N
A(p(i), j) = A(p(i), j) - z * A(p(k), j)
end do
end do
end do
do k=1, N-1
do i=k+1, N
B(p(i)) = B(p(i)) - A(p(i), k) * b(p(k))
end do
end do
do i=N, 1, -1
z = 0
do j=i+1, N
z = z + A(p(i), j) * X(j)
end do
X(i) = (B(p(i)) - z) / A(p(i), i)
end do
end subroutine
program main
implicit none
integer :: N = 3
real*8 :: A(3,3) = (/ 2, 3, 1, 1, 1, 2, 1, 2, 2 /)
real*8 :: B(3) = (/ 4, 6, 5/), X(3)
call LinearGauss (A, B, X, N)
write(*,*) X
end
!-------------------------------------------------
! 行尺度高斯消元法
!-------------------------------------------------
subroutine LinearGauss (A, B, X, N)
implicit none
integer :: N
real*8 :: A(N, N), B(N), X(N)
real*8 :: z
integer,allocatable :: p(:), s(:)
integer :: i, j, k, sl
allocate (p(N))
allocate (s(N))
do i=1, N
p(i) = i
s(i) = MaxVal ( Abs(A(i, :)) )
end do
do k=1, N-1
j = k
do i=k+1, N
if ( Abs(A(p(j), k)) / s(p(j)) < Abs(A(p(i), k)) / s(p(i)) ) then
j = i
end if
end do
sl = p(k)
p(k) = p(j)
p(j) = sl
do i=k+1, N
z = A(p(i), k) / A(p(k), k)
A(p(i), k) = z
do j=k+1, N
A(p(i), j) = A(p(i), j) - z * A(p(k), j)
end do
end do
end do
do k=1, N-1
do i=k+1, N
B(p(i)) = B(p(i)) - A(p(i), k) * b(p(k))
end do
end do
do i=N, 1, -1
z = 0
do j=i+1, N
z = z + A(p(i), j) * X(j)
end do
X(i) = (B(p(i)) - z) / A(p(i), i)
end do
end subroutine
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询