为什么我的 Python BZ Oregonator 无法编译?
Why is my Python BZ Oregonator unable to compile?
我正在尝试通过求解 ODE 来模拟 Belousov-Zhabotinsky 反应溶液中的颜色变化,并生成一个图表,该图表将使用 odeint 演示振荡。
我有一条错误消息 'AxisError: axis -1 is out of bounds for array of dimension 0',我不知道为什么会这样,我是 Python 的新手,我很挣扎。
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
# Dimensionless parameters
c1 = 10
c2 = 0.15
c3 = 0.005
c4 = 0.02
#pack 3 initial conditions with state of x,y,z into y0
y0 = [1,0,0]
k = 1
def Oregonator (t,Y):
x = Y[0]
y = Y[1]
z = Y[2]
dxdt = c1 + c2*x - x - x*y**2
dydt = (x + x*y**2 - y)/c3
dzdt = (y-z)/c4
return [dxdt,dydt,dzdt]
t = np.linspace(0, 10,100)
Y = odeint(y0,t,Oregonator)
plt.plot(t,Y)
plt.xlabel('time')
plt.ylabel('other side')
plt.show()
我正在使用 Spyder 处理 Python 代码。
感谢您提供的任何帮助,谢谢。
您需要修复对 odeint
的调用以符合其文档,至少在此处
Y = odeint(Oregonator,y0,t, tfirst=True)
并且您需要在 Oregonator
函数中平衡缩进。
然后你会得到一个振荡图。
另外,提高显示分辨率,100个点在100次振荡以上的区间太少了。由于有尖峰,每次振荡至少需要 20 个点,最好更多,因此
t = np.linspace(0, 10,5000)
我正在尝试通过求解 ODE 来模拟 Belousov-Zhabotinsky 反应溶液中的颜色变化,并生成一个图表,该图表将使用 odeint 演示振荡。
我有一条错误消息 'AxisError: axis -1 is out of bounds for array of dimension 0',我不知道为什么会这样,我是 Python 的新手,我很挣扎。
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
# Dimensionless parameters
c1 = 10
c2 = 0.15
c3 = 0.005
c4 = 0.02
#pack 3 initial conditions with state of x,y,z into y0
y0 = [1,0,0]
k = 1
def Oregonator (t,Y):
x = Y[0]
y = Y[1]
z = Y[2]
dxdt = c1 + c2*x - x - x*y**2
dydt = (x + x*y**2 - y)/c3
dzdt = (y-z)/c4
return [dxdt,dydt,dzdt]
t = np.linspace(0, 10,100)
Y = odeint(y0,t,Oregonator)
plt.plot(t,Y)
plt.xlabel('time')
plt.ylabel('other side')
plt.show()
我正在使用 Spyder 处理 Python 代码。 感谢您提供的任何帮助,谢谢。
您需要修复对 odeint
的调用以符合其文档,至少在此处
Y = odeint(Oregonator,y0,t, tfirst=True)
并且您需要在 Oregonator
函数中平衡缩进。
然后你会得到一个振荡图。
另外,提高显示分辨率,100个点在100次振荡以上的区间太少了。由于有尖峰,每次振荡至少需要 20 个点,最好更多,因此
t = np.linspace(0, 10,5000)