2017-10-14 51 views
0

我想繪製一個垂直襬動表面上彈起的球。我認爲代碼(與變量有關)正在工作,但數字並未更新以反映由於循環而變化的變量。我希望這不是模糊的問題,但他們爲什麼不更新?循環數字不更新

謝謝!

import numpy as np 
import matplotlib.pyplot as plt 


    h_b=np.array([10])#initial ball height 
    g=-9.8 #gravity acceleration 
    #v_b=np.array([0]) 
    v_b=np.array([0.0001])#initial ball velocity 
    dt=0.1 #time-step 

    a=3 #amplitude of surface's movement 
    f=0.2 #frequency of the oscillations 
    h_s=np.array([0]) #height of the oscillating surface 
    v_s=np.array([a*2*np.pi*f]) #velocity of the oscillating surface 

    r=0.9 #coefficient of restitution 

    x=np.linspace(0,20,20) 

    t=np.array([0]) # setting up the time array 


    fig= plt.figure() 
    ax1=fig.add_subplot(1,2,1) 
    ax2=fig.add_subplot(1,2,2) 
    while np.abs(h_b[-1])>0.00001 and np.abs(v_b[-1])>0.00001 : 
     while h_b[-1]>h_s[-1] : 
      n=len(v_b) 
      ax1.clear() 
      ax1.set_xlim(0,20) 
      ax1.set_ylim(-a,50) 
      ax1.plot(10,h_b[n-1],'.',x,np.ones(20)*h_s[n-1]) 
      ax2.clear() 
      ax2.plot(t,h_b) 
      plt.show() 
      v_b=np.append(v_b,v_b[n-1]+g*dt) 
      h_b=np.append(h_b,h_b[n-1]+v_b[n-1]*dt) 
      h_s=np.append(h_s,a*np.sin(2*np.pi*f*n*dt)) 
      v_s=np.append(v_s,a*2*np.pi*f*np.cos(2*np.pi*f*n*dt)) 
      t=np.append(t,n*dt) 
      plt.pause(0.1) 
     v_b[-1]=v_s[-2]-r*(v_b[-2]-v_s[-2]) 
     print('%f' % v_b[-1]) 
     h_b[-1]=h_s[-1]+0.0001 
+0

您希望實時更新,就像您可以觀看的視頻或動畫,而不是隨着時間的推移繪製球的軌跡(在靜態圖中) - 是正確的嗎? – combinatorist

+0

您可能想要參考以下問題:https://stackoverflow.com/questions/11874767/real-time-plotting-in-while-loop-with-matplotlib,https://stackoverflow.com/questions/10944621/動態更新 - 繪製在matplotlib – combinatorist

+0

@combinatorist是的,這是正確的。其他回答者說過的所有工作都很好。謝謝! –

回答

0

您不應該在循環中調用plt.show。它意味着在腳本結尾處僅被調用一次。如果你刪除plt.show(),它會按預期工作。

import numpy as np 
import matplotlib.pyplot as plt 

plt.ion() 
h_b=np.array([10])#initial ball height 
g=-9.8 #gravity acceleration 
#v_b=np.array([0]) 
v_b=np.array([0.0001])#initial ball velocity 
dt=0.1 #time-step 

a=3 #amplitude of surface's movement 
f=0.2 #frequency of the oscillations 
h_s=np.array([0]) #height of the oscillating surface 
v_s=np.array([a*2*np.pi*f]) #velocity of the oscillating surface 

r=0.9 #coefficient of restitution 

x=np.linspace(0,20,20) 

t=np.array([0]) # setting up the time array 


fig= plt.figure() 
ax1=fig.add_subplot(1,2,1) 
ax2=fig.add_subplot(1,2,2) 
plt.draw() 

while np.abs(h_b[-1])>0.00001 and np.abs(v_b[-1])>0.00001 : 

    while h_b[-1]>h_s[-1] : 
     n=len(v_b) 
     ax1.clear() 
     ax1.set_xlim(0,20) 
     ax1.set_ylim(-a,50) 
     ax1.plot(10,h_b[n-1],'.',x,np.ones(20)*h_s[n-1]) 
     ax2.clear() 
     ax2.plot(t,h_b) 

     v_b=np.append(v_b,v_b[n-1]+g*dt) 
     h_b=np.append(h_b,h_b[n-1]+v_b[n-1]*dt) 
     h_s=np.append(h_s,a*np.sin(2*np.pi*f*n*dt)) 
     v_s=np.append(v_s,a*2*np.pi*f*np.cos(2*np.pi*f*n*dt)) 
     t=np.append(t,n*dt) 
     plt.pause(0.1) 
    v_b[-1]=v_s[-2]-r*(v_b[-2]-v_s[-2]) 
    print('%f' % v_b[-1]) 
    h_b[-1]=h_s[-1]+0.0001 

plt.ioff() 
plt.show()