列选主元的高斯消元法的Fortran程序

要用到之前发的解上三角矩阵和下三角矩阵方程的模块tri_eq.f90。

博客园代码不支持fortran格式。。。

  1 module lina_gauss
  2 !---------------------------------------------module comment
  3 !  Author  :  Huang zc
  4 !  Date    :  2017-6-27
  5 !-----------------------------------------------------------
  6 !  Description:
  7 !    Solve matrix equations by variable gauss methods
  8 !-----------------------------------------------------------
  9 !
 10 use tri_eq
 11 contains
 12
 13 subroutine gauss(A,b)
 14 !---------------------------------------- subroutine comment
 15 !  Purpose  :  Solve Ax=b by gauss method
 16 !              column pivoting is used
 17 !              solution is stored in b
 18 !-----------------------------------------------------------
 19 !  Input:
 20 !       1.A(N,N) : coefficient matrix
 21 !       2.b(N)   : right-hand side vector
 22 !  Output:
 23 !       1.b(N)      : solution of matrix equation
 24 !---------------------------------------------define variable
 25 implicit none
 26 integer,parameter::iwp = selected_real_kind(15)
 27 real(iwp),parameter::eps = 3.0d-15
 28 real(iwp),intent(inout),allocatable::A(:,:)
 29 real(iwp),intent(inout),allocatable::b(:)
 30 real(iwp),allocatable::vtemp(:)
 31 real(iwp)::tmp
 32 integer::i,j,k(1),N
 33 !-------------------------------------------------------------
 34 N = size(b)
 35 allocate(vtemp(N))
 36 !--------------------------------------------print information
 37 print*,"   Subroutine gauss is being called......"
 38 print*,"   Input coefficient matrix and rhs vector:..."
 39 do i = 1,N
 40     do j = 1,N
 41         write(*,"(f10.4)",advance="no")A(i,j)
 42     enddo
 43     write(*,"(a3)",advance="no")"|"
 44     write(*,"(f10.4)")b(i)
 45 enddo
 46 !---------------------------------------------------main body
 47 do j = 1,N-1
 48     !---------------------------------pivoting within columns
 49     k = maxloc(dabs(A(j:N,j)))
 50     k(1) = k(1)+j-1
 51     if (k(1) /= j) then
 52         vtemp = A(j,:);A(j,:) = A(k(1),:);A(k(1),:) = vtemp
 53         tmp = b(j);b(j) = b(k(1));b(k(1)) = tmp
 54         print*,"   Pivoting has been used......"
 55     endif
 56     !----------------------------------------check if singular
 57     if (abs(A(j,j)) < eps) then
 58         print*,"The input coefficient matrix seems to be singular!"
 59         write(*,110)j
 60         stop "Program stop at gauss subroutine!"
 61     endif
 62     !-----------------------------------eliminate to upper tri
 63     do i = j+1,N
 64         tmp = -A(i,j)/A(j,j)
 65         A(i,j:) = A(i,j:) + tmp*A(j,j:)
 66         b(i) = b(i) + tmp*b(j)
 67     enddo
 68 enddo
 69 print*,"   The coefficient matrix has been changed to upper tri..."
 70 call uptri(A,b)
 71 !-------------------------------------------------print result
 72 print*,"   Subroutine gauss end......"
 73 110 format(t2,‘The‘,i3,‘  th pivot element is close to zero!‘)
 74 !-------------------------------------------------------------
 75 end subroutine gauss
 76
 77 subroutine gauss2(A,b,x)
 78 !---------------------------------------- subroutine comment
 79 !  Purpose  :  Solve Ax=b by gauss method with augment matix
 80 !              column pivoting is used
 81 !              solution is stored in x
 82 !-----------------------------------------------------------
 83 !  Input:
 84 !       1.A(N,N) : coefficient matrix
 85 !       2.b(N)   : right-hand side vector
 86 !  Output:
 87 !       1.x(N)      : solution of matrix equation
 88 !---------------------------------------------define variable
 89 implicit none
 90 integer,parameter::iwp = selected_real_kind(15)
 91 real(iwp),parameter::eps = 3.0d-15
 92 real(iwp),intent(in),allocatable::A(:,:)
 93 real(iwp),intent(in),allocatable::b(:)
 94 real(iwp),intent(inout),allocatable::x(:)
 95 real(iwp),allocatable::Ab(:,:),Aup(:,:),bup(:),vtemp(:)
 96 integer::i,j,k(1),N
 97 real(iwp)::tmp
 98 !-------------------------------------------------------------
 99 N = size(b)
100 allocate(Ab(N,N+1),Aup(N,N),bup(N),vtemp(N+1))
101 Ab(:,1:N) = A;Ab(:,N+1) = b
102 !--------------------------------------------print information
103 print*,"   Subroutine gauss2 is being called......"
104 print*,"   Input coefficient matrix and rhs vector:..."
105 do i = 1,N
106     do j = 1,N
107         write(*,"(f10.4)",advance="no")A(i,j)
108     enddo
109     write(*,"(a3)",advance="no")"|"
110     write(*,"(f10.4)")b(i)
111 enddo
112 !---------------------------------------------------main body
113 do j = 1,N-1
114     !---------------------------------pivoting within columns
115     k = maxloc(dabs(Ab(j:N,j)))
116     k(1) = k(1)+j-1
117     if (k(1) /= j) then
118         vtemp = Ab(j,:);Ab(j,:) = Ab(k(1),:);Ab(k(1),:) = vtemp
119         print*,"   Pivoting has been used......"
120     endif
121     !----------------------------------------check if singular
122     if (abs(Ab(j,j)) < eps) then
123         print*,"The input coefficient matrix seems to be singular!"
124         write(*,110)j
125         stop "Program stop at gauss2 subroutine!"
126     endif
127     !-----------------------------------eliminate to upper tri
128     do i = j+1,N
129         tmp = -Ab(i,j)/Ab(j,j)
130         Ab(i,j:) = Ab(i,j:) + tmp*Ab(j,j:)
131     enddo
132 enddo
133 Aup = Ab(:,1:N);bup = Ab(:,N+1)
134 call uptri(Aup,bup)
135 x = bup
136 !-------------------------------------------------print result
137 print*,"   Subroutine gauss2 end......"
138 110 format(t2,‘The‘,i3,‘  th pivot element is close to zero!‘)
139 !-------------------------------------------------------------
140 end subroutine gauss2
141
142 subroutine iter_solu(A,b,x)
143 !---------------------------------------- subroutine comment
144 !  Purpose  :  Solve Ax=b by gauss2 method and advance the accuracy
145 !              by iteration of residual equation
146 !              column pivoting is used
147 !              solution is stored in b
148 !-----------------------------------------------------------
149 !  Input:
150 !       1.A(N,N) : coefficient matrix
151 !       2.b(N)   : right-hand side vector
152 !  Output:
153 !       1.b(N)      : solution of matrix equation
154 !---------------------------------------------define variable
155 implicit none
156 integer,parameter::iwp = selected_real_kind(15)
157 integer,parameter::itmax = 6
158 real(iwp),parameter::eps = 3.0d-15
159 real(iwp),intent(in),allocatable::A(:,:)
160 real(iwp),intent(in),allocatable::b(:)
161 real(iwp),intent(inout),allocatable::x(:)
162 real(iwp),allocatable::x1(:),dx(:),x2(:),db(:)
163 integer::i,N
164 !-------------------------------------------------------------
165 N = size(b)
166 allocate(dx(N),x2(N),db(N))
167 !--------------------------------------------print information
168 print*,"   Subroutine iter_solu is being called......"
169 !---------------------------------------------------main body
170 call gauss2(A,b,x1)
171 do i = 1,itmax
172     db = matmul(A,x1) - b
173     call gauss2(A,db,dx)
174     x2 = x1-dx
175     x1 = x2
176 enddo
177 x = x1
178 !-------------------------------------------------print result
179 print*,"   Solution of the final iteration:"
180 do i = 1,N
181     write(*,"(f15.8)")x(i)
182 enddo
183 print*,"   Subroutine iter_solu end......"
184 !-------------------------------------------------------------
185 end subroutine iter_solu
186
187 end module lina_gauss
时间: 2024-10-28 12:26:13

列选主元的高斯消元法的Fortran程序的相关文章

c++调用fortran程序中遇到的问题

一.C++动态调用Fortran DLL (1)创建FORTRAN DLL工程,生成forsubs.dll文件供调用. ! forsubs.f90 ! ! FUNCTIONS/SUBROUTINES exported from FORSUBS.dll: ! FORSUBS - subroutine ! INTEGER*4 FUNCTION Fact (n) !DEC$ ATTRIBUTES DLLEXPORT::Fact INTEGER*4 n [VALUE] INTEGER*4 i, amt

Guass列选主元消去法和三角分解法

最近数值计算学了Guass列主消元法和三角分解法解线性方程组,具体原理如下: 1.Guass列选主元消去法对于AX =B 1).消元过程:将(A|B)进行变换为,其中是上三角矩阵.即: k从1到n-1 a. 列选主元 选取第k列中绝对值最大元素作为主元. b. 换行 c. 归一化 d. 消元 2).回代过程:由解出. 2.三角分解法(Doolittle分解) 将A分解为如下形式 由矩阵乘法原理 a.计算U的第一行,再计算L的第一列 b.设已求出U的1至r-1行,L的1至r-1列.先计算U的第r行

专业代写Fortran程序,Fortran程序翻译,Fortran编程一对一远程视频教学

提供专业的Fortran程序代写服务(多年的数值计算经验) QQ:2545088544,或者手机:1339零78伍489 我们团队均毕业于理工类学校,有多年的数值计算经验,现从事数值计算相关的研究工作. 精通Fortran 77/90编程,提供专业的Fortran程序代写:Fortran和其他编程语言(Matlab.C/C++.Python.VB等)之间的翻译转换:Fortran程序开发一对一教学等服务. 目前已经完成过的任务有(包括在猪八戒网接到的任务): (1)磁感应强度的数值计算模拟. (

高斯消元法(Gauss Elimination)【超详解&amp;模板】

高斯消元法,是线性代数中的一个算法,可用来求解线性方程组,并可以求出矩阵的秩,以及求出可逆方阵的逆矩阵.高斯消元法的原理是:若用初等行变换将增广矩阵 化为 ,则AX = B与CX = D是同解方程组. 所以我们可以用初等行变换把增广矩阵转换为行阶梯阵,然后回代求出方程的解. 1.线性方程组 1)构造增广矩阵,即系数矩阵A增加上常数向量b(A|b) 2)通过以交换行.某行乘以非负常数和两行相加这三种初等变化将原系统转化为更简单的三角形式(triangular form) 注:这里的初等变化可以通过

[转载] C-MEX程序编写

原作者,胡荣春 2006-10-11 1  MEX文件简介 在MATLAB中可调用的C或Fortran语言程序称为MEX文件.MATLAB可以直接把MEX文件视为它的内建函数进行调用.MEX文件是动态链接的子例程,MATLAB解释器可以自动载入并执行它. MEX文件主要有以下用途: 1.  对于大量现有的C或者Fortran程序可以无须改写成MATLAB专用的M文件格式而在MATLAB中执行. 2.  对于那些MATLAB运算速度过慢的算法,可以用C或者Frotran语言编写以提高效率. 2 

全面解析《嵌入式程序员应该知道的16个问题》

文章为转载文章,写的很好,和大家分享下,原文连接如下: ----Sailor_forever分析整理,[email protected] http://blog.csdn.net/sailor_8318/archive/2008/03/25/2215041.aspx 1.预处理器(Preprocessor) 2.如何定义宏 3.预处理器标识#error的目的是什么? 4.死循环(Infinite loops) 5.数据声明(Data declarations) 6.关键字static的作用是什么

【转】嵌入式程序员应该知道的16个问题

全面解析<嵌入式程序员应该知道的16个问题> ----Sailor_forever分析整理,[email protected] http://blog.csdn.net/sailor_8318/archive/2008/03/25/2215041.aspx 1.预处理器(Preprocessor) 2.如何定义宏 3.预处理器标识#error的目的是什么? 4.死循环(Infinite loops) 5.数据声明(Data declarations) 6.关键字static的作用是什么? 7.

Fortran编译多个文件(转载)

最近需要在Linux系统下编译多个Fortran程序,在网上搜索了一下,但是资料不多,也许因为这个问题比较简单,不值一提,但还是把我知道的写出来,供大家参考: 方法一: 假如现在有两个Fortran程序fun.f90和main.f90,其中main.f90是主程序,fun.f90是在主程序中调用的子程序,将这两个文件放到一个目录下,使用fortran编译命令,如Intel的ifort,命令如下: ifort -o exe_name fun.f90 main.f90 或者ifort -o exe_

fortran与c

http://asc.2dark.org/node/45一.说明 本文多数内容是我读彭国伦<Fortran 95 程序设计>的笔记.只读到第九章,主要是3~9 章,都是最基本的用法(原书共16章).这里主要摘录了我看书过程中总结的一些Fortran和C不 同的地方,主要是语法方面.希望这份笔记能够给学过C但没有接触过Fortran的同学带去一些帮 助.要想得更清楚些,推荐看一下原书,觉得作者真的写得很好,很清楚:如果有C语言的基础, 看完前九应该很快的,花一两天就行了.觉得如果耐心看完本文,基