    MODULE ivp_ode_rk_ex01_mod

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

    CONTAINS

      FUNCTION f(t,y)

        ! .. Implicit None Statement ..
        IMPLICIT NONE
        ! .. Intrinsic Functions ..
        INTRINSIC SIZE
        ! .. Scalar Arguments ..
        REAL (wp), INTENT (IN) :: t
        ! .. Array Arguments ..
        REAL (wp), INTENT (IN) :: y(:)
        ! .. Function Return Value ..
        REAL (wp) :: f(SIZE(y))
        ! .. Executable Statements ..
        f(1) = y(2)
        f(2) = -y(1)

      END FUNCTION f

    END MODULE ivp_ode_rk_ex01_mod

    PROGRAM nag_ivp_ode_rk_ex01

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

      ! .. Use Statements ..
      USE nag_ivp_ode_rk, ONLY : nag_rk_comm_wp => nag_rk_comm_dp, &
       nag_rk_setup, nag_rk_interval, nag_rk_info, nag_deallocate
      USE nag_math_constants, ONLY : nag_pi
      USE nag_examples_io, ONLY : nag_std_out
      USE ivp_ode_rk_ex01_mod, ONLY : wp, f
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Parameters ..
      INTEGER, PARAMETER :: n = 2
      ! .. Local Scalars ..
      INTEGER :: i, j, nout, num_fun_eval
      REAL (wp) :: pi, tinc, tol, t_end, t_got, t_start, t_want
      TYPE (nag_rk_comm_wp) :: comm
      ! .. Local Arrays ..
      REAL (wp) :: thresh(n), yp_got(n), y_got(n), y_start(n)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_ivp_ode_rk_ex01'

      pi = nag_pi(0.0_wp)

      t_start = 0.0_wp
      t_end = 2.0_wp*pi
      y_start = (/ 0.0_wp, 1.0_wp/)
      thresh = 1.0E-10_wp

      nout = 8
      tinc = (t_end-t_start)/nout

      DO i = 1, 2
        IF (i==1) tol = 1.0E-3_wp
        IF (i==2) tol = 1.0E-4_wp

        CALL nag_rk_setup(t_start,y_start,t_end,tol,thresh,comm)

        WRITE (nag_std_out,'(/1X,A,1PE8.1)') 'Calculation with tol = ', tol
        WRITE (nag_std_out,*) '   t           y1           y2'
        WRITE (nag_std_out,'(1x,F6.3,2(4X,F9.5))') t_start, y_start(:)

        DO j = nout - 1, 0, -1
          t_want = t_end - j*tinc

          CALL nag_rk_interval(f,t_want,t_got,y_got,yp_got,comm)

          WRITE (nag_std_out,'(1X,F6.3,2(4X,F9.5))') t_got, y_got(:)
        END DO

        CALL nag_rk_info(comm,num_fun_eval=num_fun_eval)

        WRITE (nag_std_out,'(1X,A,I10)') &
         'The cost of integration in evaluations of f = ', num_fun_eval
      END DO

      CALL nag_deallocate(comm)    ! Free structure allocated by NAG fl90

    END PROGRAM nag_ivp_ode_rk_ex01
