Fortran90中用subroutine列主元消去法解方程
时间: 2024-01-05 09:04:11 浏览: 115
以下是使用列主元消去法解方程组的Fortran90代码示例:
```
subroutine Gauss_elimination(A, b, n)
implicit none
real, dimension(n,n+1) :: A
real, dimension(n) :: b
integer :: n, i, j, k, p
real :: temp
do k = 1, n-1
p = k
do i = k+1, n
if (abs(A(i,k)) > abs(A(p,k))) p = i
end do
if (p /= k) then
do j = k, n+1
temp = A(k,j)
A(k,j) = A(p,j)
A(p,j) = temp
end do
end if
do i = k+1, n
temp = A(i,k) / A(k,k)
do j = k+1, n+1
A(i,j) = A(i,j) - temp * A(k,j)
end do
end do
end do
do k = n, 1, -1
b(k) = A(k,n+1)
do j = k+1, n
b(k) = b(k) - A(k,j) * b(j)
end do
b(k) = b(k) / A(k,k)
end do
end subroutine Gauss_elimination
```
在这个例子中,输入矩阵A和向量b以及它们的维度n。该代码将在原地执行列主元消去法,并计算解向量b。注意,此代码假定输入矩阵A是方阵且非奇异的。如果矩阵A不满足这些条件,则必须添加适当的错误检查。
阅读全文