fortran 逆矩阵
时间: 2023-09-04 09:15:35 浏览: 345
以下是使用Fortran编写的逆矩阵程序示例:
```
program inverse_matrix
implicit none
integer :: n, i, j, k
real :: a(10,10), b(10,10), d, t
write(*,*) 'Enter the order of matrix:'
read(*,*) n
write(*,*) 'Enter the matrix elements:'
do i = 1, n
do j = 1, n
read(*,*) a(i,j)
end do
end do
do i = 1, n
do j = 1, n
if (i == j) then
b(i,j) = 1.0
else
b(i,j) = 0.0
end if
end do
end do
do k = 1, n
d = a(k,k)
do j = 1, n
a(k,j) = a(k,j) / d
b(k,j) = b(k,j) / d
end do
do i = 1, n
if (i /= k) then
t = a(i,k)
do j = 1, n
a(i,j) = a(i,j) - t * a(k,j)
b(i,j) = b(i,j) - t * b(k,j)
end do
end if
end do
end do
write(*,*) 'The inverse matrix is:'
do i = 1, n
do j = 1, n
write(*,'(f6.2)') b(i,j)
end do
write(*,*)
end do
end program inverse_matrix
```
该程序首先读取矩阵的阶数,然后读取矩阵元素,并使用高斯-约旦消元法计算逆矩阵。最后,程序输出计算得到的逆矩阵。注意,该程序仅适用于方阵。
阅读全文