I wrote this code in Fortran:
Program RK4
implicit none
real x,y,k1,k2,k3,k4,h
integer i,n
real f
read*,n,h
x=0
y=0.001
Do i=1,n
k1=h*f(x,y)
k2=h*f(x+h/2.0, y+k1/2.0)
k3=h*f(x+h/2.0, y+k2/2.0)
k4=h*f(x+h, y+k3)
y=y+(k1+2*k2+2*k3+k4)*(1/6.0)
x=x+h
write(*,*)x,y
end do
end program
real function f(x,y)
f=1/y
end function
This code is for solving this differential equation using RK4 method:
dy/dx=1/y
I tested the code for different initial values like (x=1, y=1) and (x=0, y=0.1) and the answer was correct but for some initial values like the one in the code here (x=0,y=0.001) I do not get the right answer. Does anybody know what is the problem with the code?