## 學習目標 利用while迴圈改變物體的位置,使物體移動,並繪製出物體移動時的x-t、v-t、a-t圖。 ## 課堂練習 有一顆球的初速為(2,0,0),加速度為(-1,0,0),模擬其在五秒內的運動,並繪製出x-t、v-t、a-t圖。 ### 成果 影片連結:https://youtu.be/231jYHmZwaI ### 程式碼 ```python= Web VPython 3.2 size = 0.5 #定義變數,用來描述球體的大小 scene = canvas(width = 600, height = 400, centr = vector(0,0,0), background = vector(0.9,0.9,0.9), align = 'left') #調整視窗顯示的特性 #畫出座標軸 x = arrow (pos = vector(0,0,0) , axis = vector (1,0,0) , shaftwidth = 0.02, color = color.black) y = arrow (pos = vector(0,0,0) , axis = vector (0,1,0) , shaftwidth = 0.02, color = color.black) z = arrow (pos = vector(0,0,0) , axis = vector (0,0,1) , shaftwidth = 0.02, color = color.black) ball = sphere(pos = vector(0,0,0) , radius = size , color = color.magenta, v = vector(2,0,0), a = vector(-1,0,0)) #賦予 ball 各種屬性 pic_1 = graph(title = 'x vs t', width = 300, height = 300, xtitle = 't', ytitle = 'x', foreground = color.black,background = color.white,xmax = 5, xmin = 0, ymax = 5, ymin = -5, align = 'right') x_t = gcurve(color = color.blue) #繪製x-t圖 pic_2 = graph(title = 'v vs t', width = 300, height = 300, xtitle = 't', ytitle = 'v', foreground = color.black,background = color.white,xmax = 5, xmin = 0, ymax = 5, ymin = -5, align = 'right') v_t = gcurve(color = color.red) #繪製v-t圖 pic_3 = graph(title = 'a vs t', width = 300, height = 300, xtitle = 't', ytitle = 'a', foreground = color.black,background = color.white,xmax = 5, xmin = 0, ymax = 5, ymin = -5, align = 'right') a_t = gcurve(color = color.green) #繪製a-t圖 dt = 0.001 t = 0; while t <= 5: rate(1000) #動畫播放速率與影格數 ball.pos = ball.pos + ball.v * dt #迴圈更新物體位置 ball.v = ball.v + ball.a * dt #迴圈更新物體速度 t = t + dt x_t.plot(pos=(t,ball.pos.x)) #繪製x-t圖 v_t.plot(pos=(t,ball.v.x)) #繪製v-t圖 a_t.plot(pos=(t,ball.a.x)) #繪製a-t圖 ``` ## 進階問題——學測題的圖像化 質量為 50 kg 的某生站在電梯內的體重計上,電梯原靜止於第一樓層,電梯起動後最初 10 s 體重計的讀數均為 60 kgw,之後 20 s 體重計的讀數均為 45 kgw。若取重力加速度為 10m/s^2^,繪製此題的x-t、v-t、a-t圖。 ### 成果 影片連結:https://youtu.be/0H3QEOBNID4 ### 程式碼 ```python= Web VPython 3.2 size = 0.5 scene = canvas(width = 600, height = 400, centr = vector(0,0,0), background = vector(0.9,0.9,0.9), align = 'left') x = arrow (pos = vector(0,0,0) , axis = vector (1,0,0) , shaftwidth = 0.02, color = color.black) y = arrow (pos = vector(0,0,0) , axis = vector (0,1,0) , shaftwidth = 0.02, color = color.black) z = arrow (pos = vector(0,0,0) , axis = vector (0,0,1) , shaftwidth = 0.02, color = color.black) ball = sphere(pos = vector(0,0,0) , radius = size , color = color.magenta, v = vector(0,0,0), a = vector(-1,0,0)) pic_1 = graph(title = 'x vs t', width = 300, height = 300, xtitle = 't', ytitle = 'x', foreground = color.black, background = color.white,xmax = 30, xmin = 0, ymax = 300, ymin = 0, align = 'right') x_t = gcurve(color = color.blue) pic_2 = graph(title = 'v vs t', width = 300, height = 300, xtitle = 't', ytitle = 'v', foreground = color.black, background = color.white,xmax = 30, xmin = 0, ymax = 30, ymin = 0, align = 'right') v_t = gcurve(color = color.red) pic_3 = graph(title = 'a vs t', width = 300, height = 300, xtitle = 't', ytitle = 'a', foreground = color.black, background = color.white,xmax = 30, xmin = 0, ymax = 5, ymin = -5, align = 'right') a_t = gcurve(color = color.green) dt = 0.001 t = 0; while t <= 30: rate(1000) if t <= 10: ball.a = vector(2,0,0) #在0~10秒的加速度是+2 else: ball.a = vector(-1,0,0) #在10~30秒的加速度是-1 ball.pos = ball.pos + ball.v * dt ball.v = ball.v + ball.a * dt t = t + dt x_t.plot(pos=(t,ball.pos.x)) v_t.plot(pos=(t,ball.v.x)) a_t.plot(pos=(t,ball.a.x)) ```