- 积分
- 46372
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-9-8
- 最后登录
- 1970-1-1
成长值: 19710
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 兰溪之水 于 2011-10-23 13:10 编辑
在别人的基础上扩展加强,修正的,高斯消元法求解线性齐次方程组!!!!!
- module LinearAlgebra
- implicit none
- contains
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- !!!!!!!! !!!!!!!!!
- !!!!!!!! Gauss_Jordan法 !!!!!!!!!
- !!!!!!!! !!!!!!!!!
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- subroutine Gauss_Jordan(A,S,ANS)
- implicit none
- real :: A(:,:)
- real :: S(:)
- real :: ANS(:)
- real, allocatable :: B(:,:)
- integer :: i, N
- N = size(A,1)
- allocate(B(N,N))
- ! 保存原先的矩阵A,及数组S
- B=A
- ANS=S
- ! 把B化成对角矩阵
- call Upper(B,ANS,N) ! 把B化成上三角矩阵
- call Lower(B,ANS,N) ! 把B化成下三角矩阵
- ! 求解
- forall(i=1:N)
- ANS(i)=ANS(i)/B(i,i)
- end forall
- return
- end subroutine Gauss_Jordan
- ! 输出等式子程序
- subroutine output(M,S)
- implicit none
- real :: M(:,:), S(:)
- integer :: N,i,j
- N = size(M,1)
- ! write中加上advance="no",可以中止断行发生,使下一次的
- ! write接续在同一行当中.
- do i=1,N
- write(*,"(1x,f5.2,a1)", advance="NO") M(i,1),'A'
- do j=2,N
- if ( M(i,j) < 0 ) then
- write(*,"('-',f5.2,a1)",advance="NO") -M(i,j),char(64+j)
- else
- write(*,"('+',f5.2,a1)",advance="NO") M(i,j),char(64+j)
- end if
- end do
- write(*,"('=',f8.4)") S(i)
- end do
- return
- end subroutine output
- ! 求上三角矩阵的子程序
- subroutine Upper(M,S,N)
- implicit none
- integer :: N
- real :: M(N,N)
- real :: S(N)
- integer :: I,J
- real :: E
- do I=1,N-1
- do J=I+1,N
- E=M(J,I)/M(I,I)
- M(J,I:N)=M(J,I:N)-M(I,I:N)*E
- S(J)=S(J)-S(I)*E
- end do
- end do
- return
- end subroutine Upper
- ! 求下三角矩阵的子程序
- subroutine Lower(M,S,N)
- implicit none
- integer :: N
- real :: M(N,N)
- real :: S(N)
- integer :: I,J
- real :: E
- do I=N,2,-1
- do J=I-1,1,-1
- E=M(J,I)/M(I,I)
- M(J,1:N)=M(J,1:N)-M(I,1:N)*E
- S(J)=S(J)-S(I)*E
- end do
- end do
- return
- end subroutine Lower
- end module
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- !!!!!!!!! !!!!!!!!!
- !!!!!!!!! 求解齐次线性方程组 !!!!!!!!!
- !!!!!!!!! !!!!!!!!!
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- program main
- use LinearAlgebra
- implicit none
- integer:: N ! Size of Matrix
- real, allocatable :: A(:,:),S(:),ans(:)
- integer :: i
- write(*,*)"请输入要求方程组的元数:"
- read(*,*) N
- allocate(A(N,N))
- do i=1,N
- write(*,"(A6,I2,A30)")"输入第",i,"条方程变量的系数(用空格隔开):"
- read(*,*) A(i,:)
- end do
- allocate(S(N))
- write(*,"(A6,I2,A36)")"输入第",i,"条方程等号右边常数系数(用空格隔开):"
- read(*,*) S
- allocate(ans(N))
- write(*,*) 'Equation:'
- call output(A,S)
- call Gauss_Jordan(A,S,ANS)
- write(*,*) 'Ans:'
- do i=1,N
- write(*,"(1x,a1,'=',F8.4)") char(64+i),ANS(i)
- end do
- stop
- end program
复制代码 |
评分
-
查看全部评分
|