arrays - 具有实下标变量的数组
问题描述
我在主程序中使用子程序的结果时遇到问题。我写了这段代码:
Program RK4
implicit none
real k1,k2,k3,k4,h,t,R
integer i,n
real a
read*,n,h
t=0
R=0
Do i=1,n
call Scale_Factor(h,n,t,a)
k1=h*(1/a(t))
k2=h*(1/a(t+h/2.0))
k3=h*(1/a(t+h/2.0))
k4=h*(1/a(t+h))
t=t+h
R=R+(k1+2*k2+2*k3+k4)*(1/6.0)
write(*,*)t,R
End Do
end program
!-----------------------------------------
SUBROUTINE Scale_Factor(h,n,t,a)
implicit none
real t,a,k1,k2,k3,k4,h,g
integer i,n
t=0
a=0.001
Do i=1,n
k1=h*g(a)
k2=h*g(a+k1/2.0)
k3=h*g(a+k2/2.0)
k4=h*g(a+k3)
t=t+h
a=a+(k1+2*k2+2*k3+k4)*(1/6.0)
write(*,*)t,a
END DO
END SUBROUTINE
!-------------------------
FUNCTION g(a)
implicit none
real a,g
g=sqrt((1.0/a)+(1.0/a**2))
END FUNCTION
该子例程求解一个微分方程并a
为每个t
. 我需要在主程序中调用子程序的结果并在主程序中使用a(t)
。我想定义a(t)
为一个数组,但因为t
它是真实的,所以我做不到。谁能帮我?
解决方案
你有一个系统
R'(t)=f(a(t))
a'(t)=g(a(t))
那是半耦合的。要将这两个功能集成在一起,请使用主要形式的耦合 RK4 方法
rk4step(R,a,h)
k1a = h*g(a)
k1R = h*f(a)
k2a = h*g(a+k1a/2)
k2R = h*f(a+k1a/2)
k3a = h*g(a+k2a/2)
k3R = h*f(a+k2a/2)
k4a = h*g(a+k3a)
k4R = h*f(a+k3a)
a += (k1a+2*k2a+2*k3a+k4a)/6
R += (k1R+2*k2R+2*k3R+k4R)/6
return R,a
更好的方法是使用向量值状态和函数来避免重复类似的步骤。
推荐阅读
- database - 如何以管理员身份访问云上的 IBM DB2 仓库
- java - 如何使用 https://github.com/alamkanak/Android-Week-View 设置下个月的事件?
- c# - 只有 C# 匿名类型中允许的属性
- swift - 无法识别的选择器发送到实例,通知中心获取键盘大小
- android - 我的应用程序在第一次登录后显示空白屏幕 2 分钟
- javascript - 如何使用 jquery(或 javascript)设置关键帧 50%?
- c# - 通过 Angular 网页单点登录 (SSO) 到 Microsoft Bot v4 (WebChat)
- r - 关于制作清单时的错误
- android - 应用程序的构建类型在哪里定义?
- php - PHP - 为 SOAP 请求创建 XML