[Fortran] 纯文本查看 复制代码
module m_rk4_ods
implicit none
integer::M=100
contains
subroutine solve(func,t0,tt,y0,N)
implicit none
external func
integer::N
real*8::y0(N),y(N)
real*8::k1(N),k2(N),k3(N),k4(N)
real*8::h,tt,t0,t
integer::i
h=(tt-t0)/M
t=t0
y=y0
do i=1,M
call func(k1,t,y,N)
call func(k2,t+h/2,y+h/2*k1,N)
call func(k3,t+h/2,y+h/2*k2,N)
call func(k4,t+h,y+h*k3,N)
y=y+(k1+2*k2+2*k3+k4)*h/6
t=t0+i*h
write(11,*)t,y(1),y(2),y(3),y(4),y(5),y(6)
100 FORMAT(1X,T3,F10.5,F12.8)
end do
end subroutine solve
subroutine fun1(f,t,y,N)
implicit none
integer::N
real*8::f(N),y(N)
real*8::t,t0,tt
f(1)=cos(t-y(6))-y(3)**2/sqrt(1+y(3)**2)*cos(t-y(6))/(3*10**8)
f(2)=0
f(3)=y(1)/sqrt(1+y(1)**2)*cos(t-y(6))
f(4)=y(1)/sqrt(1+y(1)**2)
f(5)=y(2)/sqrt(1+y(2)**2)
f(6)=y(3)/sqrt(1+y(3)**2)
end subroutine fun1
end module m_rk4_ods
program main
use m_rk4_ods
implicit none
integer::N
real*8::y(6),y0(6)
real*8::t0,tt
open(unit=11,file='result10.txt')
N=6
t0=0.0
tt=12.0
y0=(/0,0,0,0,0,0/)
call solve(fun1,t0,tt,y0,N)
end program main