使用rk4-

时间:2018-03-13 05:37:48

标签: python plot ode runge-kutta

我想在写完以下内容后绘制o vs t0:

 N = 100

 t=0.0
 m = [0.0, pi/2, 0.0]

 o=[0 for j in range(0,N)]
 p=[0 for j in range(0,N)]

 for j in range(0,N):
    (t,theta) = runkut(2, t, m, 1.0/N)
    o[j] =  m[1] 
    p[j] =  m[2] 


t0 = linspace(-3*pi,3*pi,50)
plt.plot(t0,p)

我不知道如何在for循环后用新的o值绘制带有(t0)值的图形。

我收到以下错误:

    x and y must have same first dimension, but have shapes (50,) and (1000,)

由于

2 个答案:

答案 0 :(得分:0)

正如您的错误所述,t0需要与o具有相同的第一个维度长度。因此,请将t0更改为

t0 = linspace(-3*pi,3*pi,N)

答案 1 :(得分:0)

如果我正确理解了片段,那么更有机的版本将是

# define the time subdivision first
t0 = linspace(-3*pi,3*pi,N)
# use numpy facilities, as numpy is used
o = zeros(N)
p = zeros(N)
# store the initial conditions at time t0[0]
o[0] = m[1]
p[0] = m[2]
# loop over the sub-intervals
for j in range(0,N-1):
    (t,theta) = runkut(2, t0[j], m, t0[j+1]-t0[j])
    o[j+1] =  m[1] 
    p[j+1] =  m[2] 

# now t0 and o should have the same length, both by definition as well as computation
plt.plot(t0,o,t0,p)