# Try out NAG Library functions

Explore NAG maths and stats routines with interactive demos
Function ID
D02PFF
Name
nagf_ode_ivp_rkts_onestep
Description
Ordinary differential equations, initial value problem, Runge–Kutta method, integration over one step
Keywords
RK23 | RK45 | RK78 | Runge–Kutta–Merson | IVP, initial value problem | Nonstiff | first-order system
This example solves the equation
 $y′′=-y, y0=0, y′0=1$
reposed as
 $y1′=y2$
 $y2′=-y1$
over the range $\left[0,2\pi \right]$ with initial conditions ${y}_{1}=0.0$ and ${y}_{2}=1.0$. We use relative error control with threshold values of $\text{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 (${\mathbf{method}}=2$) with tolerances ${\mathbf{tol}}=\text{1.0E−4}$ and ${\mathbf{tol}}=\text{1.0E−5}$ in turn so that we may compare the solutions.
!   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 ..
Private
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
Contains
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)
Return
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,    &
totf
!     .. 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
Allocate (thres(n),iwsav(liwsav),rwsav(lrwsav),ynow(n),ypnow(n),         &
ystart(n))

!     Set initial conditions and input for D02PQF

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,  &
ifail)

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
Else
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.