Try out NAG Library functions

Explore NAG maths and stats routines with interactive demos
Function ID
Solution of real sparse nonsymmetric linear system, RGMRES, CGS, Bi-CGSTAB or TFQMR method, preconditioner computed by
real, sparse matrix | Bi-CGSTAB | CGS, conjugate gradient method | RGMRES, restarted generalized minimum residual method | TFQMR, transpose-free quasi-minimal residual method | incomplete LU factorization | iterative methods, linear equations | linear equations, iterative method
This example solves a sparse linear system of equations using the CGS method, with incomplete LU preconditioning.
The example data reflects that shown in the "Example" section of the routine documentation. You can change this here to try alternative inputs. The formatting will need to be kept as it is here, otherwise the program is likely to fail to run correctly.

Please note that incompatible data will however cause the example output to display an error message. These error messages are fully explained in the Routine document
    Program f11dcfe

!     F11DCF Example Program Text

!     Mark 26.1 Release. NAG Copyright 2016.

!     .. Use Statements ..
      Use nag_library, Only: f11daf, f11dcf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: dtol, rnorm, tol
      Integer                          :: i, ifail, itn, la, lfill, liwork,    &
                                          lwork, m, maxitn, n, nnz, nnzc,      &
      Character (8)                    :: method
      Character (1)                    :: milu, pstrat
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:), b(:), work(:), x(:)
      Integer, Allocatable             :: icol(:), idiag(:), ipivp(:),         &
                                          ipivq(:), irow(:), istr(:), iwork(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max
!     .. Executable Statements ..
      Write (nout,*) 'F11DCF Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)

!     Read algorithmic parameters

      Read (nin,*) n, m
      Read (nin,*) nnz
      la = 3*nnz
      liwork = 7*n + 2
      lwork = max(4*n+m*(m+n+5)+101,8*n+100,2*n*(m+3)+m*(m+2)+100,11*n+100)
      Allocate (a(la),b(n),work(lwork),x(n),icol(la),idiag(n),ipivp(n),        &
      Read (nin,*) method
      Read (nin,*) lfill, dtol
      Read (nin,*) pstrat
      Read (nin,*) milu
      Read (nin,*) tol, maxitn

!     Read the matrix A

      Do i = 1, nnz
        Read (nin,*) a(i), irow(i), icol(i)
      End Do

!     Read right-hand side vector b and initial approximate solution x

      Read (nin,*) b(1:n)
      Read (nin,*) x(1:n)

!     Calculate incomplete LU factorization

!     ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      ifail = 0
      Call f11daf(n,nnz,a,la,irow,icol,lfill,dtol,pstrat,milu,ipivp,ipivq,     &

!     Solve Ax = b using F11DCF

      ifail = 0
      Call f11dcf(method,n,nnz,a,la,irow,icol,ipivp,ipivq,istr,idiag,b,m,tol,  &

      Write (nout,99999) itn
      If (rnorm<tol) Then
        Write (nout,99996) tol
        Write (nout,99998) rnorm
      End If
      Write (nout,*)

!     Output solution, x

      Write (nout,*) '       Solution'
      Write (nout,99997) x(1:n)

99999 Format (1X,' Converged in',I4,' iterations')
99998 Format (1X,' Final residual norm =',1P,E15.2)
99997 Format (1X,1P,E16.3)
99996 Format (1X,' Final residual norm < tolerance (',1P,E10.3,')')
    End Program f11dcfe
The NAG Library
The world’s largest collection of robust, documented, tested and maintained numerical algorithms.
Learn more here or contact us for purchasing information