I have a question about solving the first order complex differential equation. I used Runge-Kutta but the answer does not seem to be right.
This is my equation:
y'=exp(-2*t)-i*y
The results for ODEs are OK but for complex equations it does not seem to be correct.
I compiled my code with gfortran and I plotted the diagram with gnuplot for ordinary differential equation without i
. The diagram was correct for the solution of the equation. but I added i
in my equation and the diagram was just a straight line. Also, gnuplot just draw the real part.
The code was compiled without any compile-time errors.
This is my code:
program kutta
implicit none
real:: a=0.0, b=8.0,y=0.1,h,t
integer::n=40
complex::i=(0,1)
interface
function f(t,y)
real::t,y
end function f
end interface
h=(b-a)/real(n)
t=a
call rk4(f,t,y,h,n)
end program kutta
function f(t,y)
f=(exp(-2*t)-2*y*i)
end function f
subroutine rk4(f,t,y,h,n)
real::f1,f2,f3,f4,t1,y1
real::t,y,h
integer::k
interface
function f(t,y)
real::t,y
end function f
end interface
t1=t
y1=y
do k=1,n
f1=h*f(t,y)
f2=h*f(t+h/2.0,y+f1/2)
f3=h*f(t+h/2.0,y+f2/2)
f4=h*f(t+h,y+f3)
y=y1+(f1+2*f2+2*f3+f4)/6.0
t=t1+h*real(k)
open(unit=1,file='data.dat')
write(1,*) t, y
!print*,k,t,y
end do
close (unit=1)
end subroutine rk4
and the output:
0.200000003 1.22892008E+14
0.400000006 1.46381659E+29
0.600000024 NaN
0.800000012 NaN
1.00000000 NaN
....