0
votes

I want to plot of o vs t0 after I have written the following:

 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)

I am not sure how can I plot a graph with (t0) values with the new o values after the for loop.

I got the following error:

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

Thanks

2
I'm not sure I get it... Why would plt.plot(t0,o) not be enough? - Fred
It's giving an error, namely x and y must have same first dimension, but have shapes (50,) and (1000,) - ak19
Thanks Fred, have added it now - ak19

2 Answers

0
votes

As your error states, t0 needs to have the same first dimension length as o. So change your t0 to

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

If I understand the fragment correctly, a more organic version would be

# 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)