【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《fortran语言解n元线性方程组》,欢迎阅读!
PROGRAM MAIN
IMPLICIT NONE INTEGER I,J,K,N REAL:: SUM=0
REAL,ALLOCATABLE::A(:,:)
REAL,ALLOCATABLE::B(:)
REAL,ALLOCATABLE::X(:) PRINT*,'请输入方程组未知数个数' READ*,N
ALLOCATE(A(N,N+1))
ALLOCATE(B(N+1))
ALLOCATE(X(N))
PRINT*,'请按列输入方程组的系数和列向量b(即输入增广矩阵A)' READ*,A DO K=1,N-1
DO WHILE(A(K,K)==0) B(:)=A(K,:) DO I=K,N-1 A(I,:)=A(I+1,:) END DO A(N,:)=B(:) END DO
DO I=K+1,N
A(I,:)=A(I,:)-A(I,K)/A(K,K)*A(K,:)
END DO
END DO
X(N)=A(N,N+1)/A(N,N) DO I=N-1,1,-1
DO J=I+1,N
SUM=SUM+A(I,J)*X(J)
END DO
X(I)=(A(I,N+1)-SUM)/A(I,I)
SUM=0
END DO PRINT*,X
END
用其他语言编写的话,算法是一样的,只是要改一下其中的循环结构
本文来源:https://www.wddqxz.cn/81f009acbfeb19e8b8f67c1cfad6195f312be801.html