fortran实战手册(5)

发布时间 2023-12-03 23:05:57作者: 水宝石

高斯消元法


program hello
    use gauss
    implicit none
    integer,parameter::mat_n=3
    real,dimension(mat_n)::roots
    real,dimension(mat_n)::b=[3,3,-6]
    character(len=50)::a_format
    real,dimension(mat_n,mat_n)::a=reshape([1,2,-3,2,1,1,-1,-2,1],[mat_n,mat_n],order=[1,2])
    integer::err_flag
    character(len=50)::err_msg
    write (a_format,"(I1,(A9))") mat_n,"(F6.2,1x)"


    call gauss_roots(a,b,mat_n,roots,err_flag,err_msg)
    if (err_flag>0) then
        write (*,*) "error:",err_msg
    else
        a=reshape(a,[mat_n,mat_n],order=[2,1])
        write (*,*) "a="
        write (*,"("//a_format//")") a
        write (*,*) "b="
        write (*,*) b
        write (*,*) "x="
        write (*,*) roots
    end if
end program

 a=
  1.00   2.00  -1.00
  0.00  -3.00   0.00
  0.00   0.00  -2.00
 b=
   3.00000000      -3.00000000      -4.00000000
 x=
   3.00000000       1.00000000       2.00000000

Process returned 0 (0x0)   execution time : 0.182 s
Press any key to continue.