我目前正試圖弄清楚如何在程序中存儲兩組值t和y,以便我可以在圖上繪製這些數據點。我相信正確的方法是使用數組,但我不知道如何繼續。在python中使用數組
import numpy as np
import matplotlib.pyplot as plt
t = 0.0
y = 0.0
u = 0.0
F = 0.2
Wd = 2*3.14
w0 = 1.5*Wd
b = w0/4
h= 0.05
while (t <= 5.95):
m1 = u
k1 = (-w0**2)*np.sin(y) + u*(1-2*b) + F*(w0**2)*np.cos(Wd*t)
m2 = u + (h/2.) * k1
t_2 = t + (h/2.)
y_2 = y +(h/2.) * m1
u_2 = m2
k2 = (-w0**2)*np.sin(y_2) + u_2*(1-2*b) + F*(w0**2)*np.cos(Wd*t_2)
m3 = u + (h/2.) * k2
t_3 = t + (h/2.)
y_3 = y + (h/2.) * m2
u_3 = m3
k3 = (-w0**2)*np.sin(y_3) + u_3*(1-2*b) + F*(w0**2)*np.cos(Wd*t_3)
m4 = u + h * k3
t_4 = t + h
y_4 = y + h * m3
u_4 = m4
k4 = (-w0**2)*np.sin(y_4) + u_4*(1-2*b) + F*(w0**2)*np.cos(Wd*t_4)
t = t + h
y = y + (h/6.) * (m1 + (2 * m2) + (2 * m3) + m4)
u = u + (h/6.) * (k1 + (2 * k2) + (2 * k3) + k4)
print t, y
那麼,什麼不起作用?你在掙扎什麼? – martijnn2008
我想從我的Python代碼中獲取打印值,並將它們繪製在圖形上,其中t是x軸,y是y軸。 – Cosmoman