Try out NAG Library functions

Explore NAG maths and stats routines with interactive demos
Function ID
Ordinary differential equations, initial value problem, Runge–Kutta method, integration over one step
RK23 | RK45 | RK78 | Runge–Kutta–Merson | IVP, initial value problem | Nonstiff | first-order system
This example solves the equation
y=-y,  y0=0,  y0=1  
reposed as
over the range 0,2π with initial conditions y1=0.0 and y2=1.0. We use relative error control with threshold values of 1.0E−8 for each solution component and print the solution at each integration step across the range. We use a medium order Runge–Kutta method (method=2) with tolerances tol=1.0E−4 and tol=1.0E−5 in turn so that we may compare the solutions.
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
!   D02PFF Example Program Text
!   Mark 26.1 Release. NAG Copyright 2016.

    Module d02pffe_mod

!     D02PFF Example Program Module:
!            Parameters and User-defined Routines

!     .. Use Statements ..
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Public                           :: f
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter, Public :: tol1 = 1.0E-4_nag_wp
      Real (Kind=nag_wp), Parameter, Public :: tol2 = 1.0E-5_nag_wp
      Integer, Parameter, Public       :: liwsav = 130, n = 2, nin = 5,        &
                                          nout = 6
      Integer, Parameter, Public       :: lrwsav = 350 + 32*n
      Subroutine f(t,n,y,yp,iuser,ruser)

!       .. Scalar Arguments ..
        Real (Kind=nag_wp), Intent (In) :: t
        Integer, Intent (In)           :: n
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
        Real (Kind=nag_wp), Intent (In) :: y(n)
        Real (Kind=nag_wp), Intent (Out) :: yp(n)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Executable Statements ..
        yp(1) = y(2)
        yp(2) = -y(1)
      End Subroutine f
    End Module d02pffe_mod

    Program d02pffe

!     D02PFF Example Main Program

!     .. Use Statements ..
      Use d02pffe_mod, Only: f, liwsav, lrwsav, n, nin, nout, tol1, tol2
      Use nag_library, Only: d02pff, d02pqf, d02ptf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: hnext, hstart, tend, tnow, tol,      &
                                          tstart, waste
      Integer                          :: i, ifail, method, stpcst, stpsok,    &
!     .. Local Arrays ..
      Real (Kind=nag_wp)               :: ruser(1)
      Real (Kind=nag_wp), Allocatable  :: rwsav(:), thres(:), ynow(:),         &
                                          ypnow(:), ystart(:)
      Integer                          :: iuser(1)
      Integer, Allocatable             :: iwsav(:)
!     .. Executable Statements ..
      Write (nout,*) 'D02PFF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) method
      Allocate (thres(n),iwsav(liwsav),rwsav(lrwsav),ynow(n),ypnow(n),         &

!     Set initial conditions and input for D02PQF

      Read (nin,*) tstart, tend
      Read (nin,*) ystart(1:n)
      Read (nin,*) hstart
      Read (nin,*) thres(1:n)

      Do i = 1, 2
        If (i==1) Then
          tol = tol1
        End If
        If (i==2) Then
          tol = tol2
        End If

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call d02pqf(n,tstart,tend,ystart,tol,thres,method,hstart,iwsav,rwsav,  &

        Write (nout,99999) tol
        Write (nout,99998)
        Write (nout,99997) tstart, ystart(1:n)

loop:   Do
          ifail = 0
          Call d02pff(f,n,tnow,ynow,ypnow,iuser,ruser,iwsav,rwsav,ifail)

          If (ifail==0) Then
            Write (nout,99997) tnow, ynow(1:n)
            If (tnow>=tend) Then
              Exit loop
            End If
            Exit loop
          End If

        End Do loop

        ifail = 0
        Call d02ptf(totf,stpcst,waste,stpsok,hnext,iwsav,rwsav,ifail)
        Write (nout,99996) totf
      End Do

99999 Format (/,' Calculation with TOL = ',E8.1)
99998 Format (/,'    t         y1        y2',/)
99997 Format (1X,F6.3,2(3X,F8.4))
99996 Format (/,' Cost of the integration in evaluations of F is',I6)
    End Program d02pffe
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