## Vpython-加速度運動四部曲
#### by 蘇昱全 Switch
---
## 第一部分:自由落體
----
## 物理概念
----
當一個物體只受重力作用,而無其他外力影響,則稱此過程為
### 自由落體
---
## 語法
----
### 物件
今天要來新學的物件是球體(Sphere)
```
Ball = sphere(pos, radius, velocity, acceleration, color)
```
由於今天的模擬講白了就是等加速度運動,所以velocity 和 acceleration 都要加入。
----
還有一個可愛的物件的小裝飾叫做texture
就是在定義物件時多加一項,如下
```
Ball = sphere(pos, radius, velocity, acceleration, tesxure)
```
註:這次我們用的是texture = texture.earth
----
### 畫圖
```
Graphname = graph(title , width, height, x, y, xtitle, ytitle, xmax, xmin, ymax, ymin)
```
如果沒有指定最大最小值,那他會根據你的值設定合適的最大最小值。
----
前頁的程式僅僅是定義好一個底圖,要在上面標示點、曲線等,還需要再一行程式。
```vpython
Curvename = gcurve(graph , color)
```
其中的graph項就是在說你要在哪一張底圖上畫圖
----
而在模擬中,我們會把每一瞬間的值記錄在圖上,所以大概會長這個樣子:
```python=
switch = graph(title...)
exp = gcurve(graph = switch, color = color.cyan)
while ......:
rate = (1000)
ball.v += ......
.
.
.
exp.plot(pos = (t,ball.v)) #畫圖(示例為v-t圖)
#plotname.plot(pos = (x, y))
t = t + dt
```
切記喔!畫plot的時候,pos不是用vector
----
### 補充
我們先前有說,Vpython裡很多物理量都是向量,那我們如果只是要取大小,不管方向呢?
```python
ball.v = vec(a,b,c)
lenth = (a^2+b^2+c^2)^0.5 #太慢,還有可能算錯
lenth = mag(ball.v) #內建的取長度工具
```
---
### 實作
----
### 本次實作內容:自由落體,標示x-t、v-t、a-t圖
----
#### 第一步老樣子,要先定義一些要用的數字
```=
from vpython import * #如果是線下版的必須加這句
r = 2 #球半徑
L = 40 #地板長度
g = vec(0,-9.8,0) #我們必須區分上下,“ - ” 代表方向向下
m = 1 #物體質量(not so nessesary)
h = 10 #落下高度
dt = 0.001 #時間間隔
t = 0 #時間
```
----
小叮嚀:
- 你得注意每個物理量的方向(向量)
- “質量“不是每一次都是可有可無
- 雖然沒寫,我們默認所有物理量以SI制呈現
----
#### 建立環境、物件
```vpython=11
scene = canvas(title = "Free Fall", width = 400, height = 400, x = 0, y = 0, center = vec(0,10,0), background = color.cyan)
floor = box(pos = vec(0,0,0), size = vec(L,0.05*L,10), color = color.green)
ball = sphere(pos = vec(0,h,0), radius = r, v = vec(0,0,0), a = vec(0,0,0), texture = texture.earth)
```
----
#### 建立x-t、v-t、a-t圖
```vpython=15
g1 = graph(title = "x-t plot", width = 400, height = 400, x = 0, y = 0, xtitle = "t(s)", ytitle = "x(m)")
g2 = graph(title = "v-t plot", width = 400, height = 400, x = 0, y = 0, xtitle = "t(s)", ytitle = "v(m/s)")
g3 = graph(title = "a-t plot", width = 400, height = 400, x = 0, y = 0, xtitle = "t(s)", ytitle = "a(m/s^2)")
xt = gcurve(graph = g1, color = color.blue)
vt = gcurve(graph = g2, color = color.blue)
at = gcurve(graph = g3, color = color.blue)
```
----
#### 物理模擬
```=22
while ball.pos.y - r >= floor.pos.y + 0.05*L/2:
rate(1000)
F = m*g
ball.pos += ball.v*dt
ball.a = F/m
ball.v += ball.a*dt
xt.plot(pos = (t,mag(ball.pos)))
vt.plot(pos = (t,mag(ball.v)))
at.plot(pos = (t,mag(ball.a)))
t = t + dt
```
---
[成果演示](https://www.glowscript.org/#/user/andysu960816/folder/MyPrograms/program/Tutorial-FreeFall)
---
# 謝謝大家
{"title":"Vpython-自由落體","breaks":true,"contributors":"[{\"id\":\"084e105f-92be-4605-b399-8d3c0ef40c64\",\"add\":6610,\"del\":3809}]"}