    MODULE nlin_sys_ex01_mod

      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Default Accessibility ..
      PUBLIC
      ! .. Intrinsic Functions ..
      INTRINSIC KIND
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)

    CONTAINS

      SUBROUTINE fun(x,finish,f_vec,f_jac)

        ! .. Implicit None Statement ..
        IMPLICIT NONE
        ! .. Intrinsic Functions ..
        INTRINSIC SIZE
        ! .. Scalar Arguments ..
        LOGICAL, INTENT (INOUT) :: finish
        ! .. Array Arguments ..
        REAL (wp), OPTIONAL, INTENT (OUT) :: f_jac(:,:)
        REAL (wp), INTENT (OUT) :: f_vec(:)
        REAL (wp), INTENT (IN) :: x(:)
        ! .. Local Scalars ..
        INTEGER :: n
        ! .. Executable Statements ..
        n = SIZE(x)

        f_vec = (3.0_wp-2.0_wp*x)*x + 1.0_wp
        f_vec(2:n) = f_vec(2:n) - x(1:n-1)
        f_vec(1:n-1) = f_vec(1:n-1) - 2.0_wp*x(2:n)

      END SUBROUTINE fun

    END MODULE nlin_sys_ex01_mod

    PROGRAM nag_nlin_sys_ex01

      ! Example Program Text for nag_nlin_sys
      ! NAG fl90, Release 4. NAG Copyright 2000.

      ! .. Use Statements ..
      USE nag_examples_io, ONLY : nag_std_out
      USE nag_nlin_sys, ONLY : nag_nlin_sys_sol
      USE nlin_sys_ex01_mod, ONLY : fun, wp
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Parameters ..
      INTEGER, PARAMETER :: n = 9
      ! .. Local Arrays ..
      REAL (wp) :: x(n)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_nlin_sys_ex01'

      ! Starting values for the initial approximate solution
      x = -0.1_wp

      ! Solve the system of non-linear equations

      CALL nag_nlin_sys_sol(fun,x,user_jac=.FALSE.)

      WRITE (nag_std_out,'(/1X,A/3(/3F12.4))') 'Final approximate solution', x

    END PROGRAM nag_nlin_sys_ex01
