为什么这个Runge-Kutta二阶程序打印出错误的值?

时间:2017-11-19 17:20:17

标签: fortran gfortran fortran90

我最近发布了一个关于此代码与其背后概念相关的问题,但我现在对此进行了排序。但是,代码打印出错误的值,我无法找出原因。这是代码:

program rungekutta
implicit none
integer, parameter :: dp = selected_real_kind(15,300)
integer :: i
real(kind=dp) z, y, t
do i=1,100
call rk2(dydt, dzdt, z, y, t)
print *, z, y
end do

contains
subroutine rk2(z, y, t)
implicit none
integer, parameter :: dp = selected_real_kind(15,300)
real(kind=dp) :: k1y, k1z, k2y, k2z, n, h=0.1, y=1, z=0, t
k1y = dydt(y,z,t)*h
k1z = dzdt(y,z,t)*h
k2z = dzdt(y + (0.5*k1y), z + (0.5*k1z), t + (0.5*h))*h
k2y = dydt(y, z +(0.5*k1z), t)*h
y = y + k2y
z = z + k2z
end subroutine

function dzdt(y,z,t)
real(kind=dp) :: y, z, t, dzdt, omega=1, A=0, B, C=0, D=0
B = omega**2
dzdt = A*y**3 + B*y - C*z + D*sin(omega*t)
end function

function dydt(z)
real(kind=dp) ::  z, dydt
dydt = z
end function
end program

以下是终端的打印屏幕:https://prnt.sc/hchmv7

任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

看起来你用5个参数调用rk2,但只用3

声明它