Program

c	Runge Kutta for first order differential equations
c
 	PROGRAM Runge-Kutta
 	IMPLICIT none
c
c 		declarations
c nsteps:number of steps, tstep:length of steps, y: initial position
c
	REAL*8 t, y, tstep
	INTEGER	i, j, nsteps
	nsteps=10
        tstep=0.5
        y=1.0
c
c          	open file
        OPEN(6, FILE='rungef.dat')
        WRITE (6,*) 0, y
c
c 		do loop nsteps of Runga-Kutta algorithm
	DO 60 j = 1, nsteps
	t=j*tstep
   	call rk4(t, y, tstep)
        WRITE (6,*) t, y
 60	CONTINUE
c
	CLOSE(6)
	STOP
        END
c------------------------end of main program------------------------
c
c 		fourth-order Runge-Kutta subroutine 
	SUBROUTINE rk4(t, y, tstep)
	IMPLICIT none
c
c		declarations
	REAL*8 DERIV, h, t, tstep, y 
        REAL*8 k1, k2, k3, k4
     	INTEGER i, N
     	h=tstep/2.0
c
	k1 = tstep * DERIV(t, y)
	k2 = tstep * DERIV(t+h, y+k1/2.0)
	k3 = tstep * DERIV(t+h, y+k2/2.0)
	k4 = tstep * DERIV(t+tstep, y+k3)
c
	y = y + (k1 + (2.*(k2 + k3)) + k4)/6.0
c 
	RETURN
	END
c
c		function which returns the derivatives
	FUNCTION DERIV(t, temp)
	IMPLICIT none
c
c		declarations
	REAL*8 DERIV, t, temp
	INTEGER i
c
	DERIV=-temp
c	
	RETURN
	END

A source code which you can save and run on your computer.
Back to main document.