使用 RK4 进行仿真,随着仿真的进行更新 ODE 变量

Simulation with RK4, update ODE variable as the simulation goes

问题:

我目前正在制作一个 python 应用程序来模拟一组依赖于变量的耦合常微分方程,我们称它为 « X »。

至于现在,我基本上是在给定时间内用 RK4 模拟这组 ODE,然后我用 tkinter 中嵌入的“matplotlib 动画”在动画图上绘制图形。

事实是,我希望能够在求解方程式时修改 « X »,以便模拟可以随着我们修改此变量而改变。

上下文:

从那时起解决方案发生了变化。综上所述,这些是氙和碘丰度以及核反应堆中中子流的方程式。变化的变量是控制控制条的 "big sigma_b"。

颂歌:

氙和碘丰度 ODE

中子流 ODE :

总结:

总而言之,« X » 属于范围 [1.0, 2.0],假设我们想要 运行 400 小时的模拟:

希望已经足够清楚了。 我该怎么做?

将评论的想法转化为模型提供了一些代码来进行试验

import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
import matplotlib.animation as anim
import numpy as np
from scipy.integrate import odeint

# model is dampened oscillation with control variable u 
# representing the equilibrium position
# x'' + 0.4*x + x == u

def model(x,u): return np.array([x[1], u - 0.4*x[1] - x[0]])

# integrate over about 10 periods
N=250
t = np.linspace(0, 12*np.pi,  N+1)              # time
# arrays to hold the computed values
X = np.zeros([N+1,2])
U = np.zeros([N+1])
# initial values
X[0] = [3, 2]          
U[0] = 2;

# initialize plot
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
# initialize graphs
l1, = plt.plot(t[0], X[0,0], '-+b', ms=2 )
l2, = plt.plot(t[0], U[0], '-or', ms=2, lw=1 )
plt.xlim(t[0], t[-1]); plt.ylim(-1,3)

# construct slider
axcolor = 'black'
ax_U = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
sU = Slider(ax_U, 'U', 1.0, 2.0, valinit=U[0])


def animate(i):
    # read the slider value
    U[i] = sU.val
    # integrate over the sub-interval
    X[i] = odeint(lambda x,t: model(x,U[i]), X[i-1], [t[i-1],t[i]], atol=1e-4, rtol=1e-8)[-1];
    # set the data to plot
    l1.set_data(t[0:i+1],X[0:i+1,0]);
    l2.set_data(t[0:i+1],U[0:i+1]);
    return l1,l2

# start the animation, one should set the parameter to keep it from repeating
anim = anim.FuncAnimation(fig, animate, frames = range(1,N+1), blit=True, interval = 100 )
plt.show()