RUNGE-KUTTA

 !Program name: RK

!Runge-Kutta method for differential equation dy/dx=-xy

!The exact solution: y=exp(-x^2/2)

!Interval X=0 to X=3 where y(0)=1

!Y(n+1) <=> X+h

 

program Runge_Kutta

real :: x, y, h, exact, error

Real :: k1, k2, k3, k4

integer :: i

f(x,y)=-x*y

 

!fourth order Runge-Kutta

h=0.05

nstep=3./h  !60 steps

y=1.   !y(0)=1

do i=0,nstep

   x=h*i

   k1=h*f(x,y)

   k2=h*f(x+0.5*h,y+0.5*k1)

   k3=h*f(x+0.5*h,y+0.5*k2)

   k4=h*f(x+h,y+k3)

   y=y+(1/6.*(k1+2.*k2+2.*k3+k4))

   exact=exp((-(x+h)**2)/2)

   error=exact-y

   print *, i, x+h, y,exact, error

!for h=0.05  y(1)=0.60653    error=-0.00000

!for h=0.05  y(3)=0.00111    error=-7.63E-08

end do

Print *,

 

!Second order Runge Kutta

h=0.05

nstep=3./h  !60 steps

y=1.   !y(0)=1

do i=0,nstep

   x=i*h    !It guarantees interval from 0 to 3

   k=h*f(x,y)

   y=y+h*f(x+0.5*h,y+0.5*k)

   exact=exp((-(x+h)**2)/2)

   error=exact-y

   print *, i, x+h, y,exact, error

!for h=0.05  y(1)=0.60137     error=--0.00515

!for h=0.05  y(3)=0.00871    error=-0.00240

end do

stop

end

Comentários

Postagens mais visitadas deste blog

HYDROGEN-RADIAL

HYDROGEN-POTENTIAL

ONE_PART_QHO