0

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

ak19
  • 9
  • 4

2 Answers2

0

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)
Fred
  • 1,462
  • 8
  • 15
0

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)
Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51