重力場及電場
作者:王一哲
日期:2018/5/7
若在空間中有一個質量為 且質量均勻分布的球體,若以 的球心為原點,在空間中位置向量為 處的重力場為
上式中的負號帶表重力場方向指向球心。若有多個球體,第 個球體質量為 、球心位置為 ,則重力場為
如果要在黑板上畫出各個位置的重力場強度及方向,這幾乎是不可能的任務,下圖是我畫出來的地球重力場示意圖
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
地球重力場示意圖
在以下的課程中,我們想要在空間每隔一段距離取一個點,以箭頭的長度及方向來表示該點的重力場。因此我們在程式 18-1 中先練習使用 for 迴圈,在空間中每隔一段距離畫一個箭頭。在程式 18-2 中除了畫出箭頭還要計算此處的重力場,再更新箭頭的長度及方向。如果能夠成功畫出一個球體的重力場,在程式 18-3 中則更進一步地畫出兩個球體,甚至
們可以用相同的方法畫出帶電球體在空間中建立的電場,其數學型式為
我們只要稍微修改程式 18-3 就能畫出兩個帶電球體在空間中建立的電場。
這個程式與之前的程式相比十分簡短,只要將箭頭畫完就可以了,因此連產生動畫的部分都沒有。比較特別之處在於使用了 3 層的 for 迴圈,第 1 層迴圈的變數是 i ,用來產生位置的 x 坐標;第 2 層迴圈的變數是 j ,用來產生位置的 y 坐標;第 3 層迴圈的變數是 k ,用來產生位置的 z 坐標。由於使用了 range(N)、N = 4 來產生數值,也就是只會產生 0、1、2、3,所以畫箭頭的位置為 -L/2、-L/4、0、L/4。程式執行的成果如下圖。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
程式18-1畫面截圖
由於我們在之前的課程〈行星運動〉當中已經蒐集了不少行星資料,並且在程式 12-4 中學到如何利用 class 產生自訂類別的物件,因此我們可以將程式 12-4 和 18-1 合併,寫出程式 18-2。主要的步驟為:
- 參數設定
- 自訂類別 Planet,並在當中新增自訂的方法(method) g,用來回傳此物件在輸入的位置 pos 處產生的重力場。
- 產生動畫視窗、地球。
- 計算畫箭頭的位置,如果不在地球內則加到串列 locations 當中。
- 依序讀取串列 locations 的元素,在對應的位置產生箭頭。
- 第 49 ~ 55 行,依序讀取箭頭的位置,計算此處的重力場,更新箭頭的長度及方向。由於重力場的量值與畫面的寬度相比太短,因此需要將重力場乘以 1 × 106 才能在畫面上看到箭頭。將重力場量值當中的最大值記錄為 fmax,量值越大箭頭的顏色越接近紅色,量值越小箭頭的顏色越接近綠色。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
程式18-2畫面截圖
程式 18-3 與 18-2 幾乎一模一樣,只是多畫了火星。由於要產生兩個行星,在此應該就能看出自訂類別較為方便之處。需要特別注意一點,兩個行星之間的距離並未按照真實數據繪製,否則畫面中什麼東西都看不到。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
程式18-3畫面截圖
"""
VPython教學: 18-4. 電場, 2個球狀帶電體
Ver. 1: 2018/3/2
Ver. 2: 2019/9/14 箭頭的顏色隨著量值改變, 改為不用繼承的類別
作者: 王一哲
"""
from vpython import *
"""
1. 參數設定, 設定變數及初始值
"""
size = 1
d = 10
L = 1.8*d
q1, c1 = 5, color.blue
q2, c2 = -5, color.red
ke = 8.988E9
N = 6
"""
2. 產生帶電球體類別, 回傳帶電球體產生的電場
"""
class Ball:
def __init__(self, pos, radius, color, charge):
self.pos = pos
self.radius = radius
self.color = color
self.charge = charge
self.ball = sphere(pos=vec(self.pos), radius=self.radius, charge=self.charge, color=self.color)
def electric(self, pos2):
return ke*self.charge / mag2(pos2-self.pos) * norm(pos2-self.pos)
"""
3. 畫面設定
"""
scene = canvas(title="Electric Field", width=600, height=600, x=0, y=0, background=color.black, range=L)
b1 = Ball(pos=vec(-d/2, 0, 0), radius=size, color=c1, charge=q1)
b2 = Ball(pos=vec(d/2, 0, 0), radius=size, color=c2, charge=q2)
locations = []
for i in range(N+1):
for j in range(N+1):
for k in range(N+1):
location = vec(L/N*i - L/2, L/N*j - L/2, L/N*k - L/2)
if mag(location-b1.pos) > 2*size and mag(location-b2.pos) > 2*size:
locations.append(location)
fields = []
for location in locations:
fields.append(arrow(pos=location, axis=vec(0, 0, 0), color=color.green))
fmax = 0
for field in fields:
field.axis = (b1.electric(field.pos) + b2.electric(field.pos))*1E-9
if field.axis.mag >= fmax: fmax = field.axis.mag
for field in fields:
field.color = vec(field.axis.mag/fmax, 1 - field.axis.mag/fmax, 0)
程式 18-4 與 18-3 幾乎一模一樣,以下只解釋不同之處:
- 將球體半徑、畫面寬度大幅縮小,設定帶電球體的電量、靜電力常數。
- 第 23 ~ 31 行,自訂類別 Ball 時不採用繼承的方式,產生此類別的物件時要輸入位置 pos、半徑 radius、顏色 color、電量 charge,產生一個球體,並且自訂計算電場用的函式 electric。
- 若左側小球電量為 q1、右側小球電量為 q2,以下是 3 種不同組合的模擬結果。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
q1 = 1, q2 = -5 模擬結果畫面截圖
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
q1 = 5, q2 = -5 模擬結果畫面截圖
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
q1 = 5, q2 = -1 模擬結果畫面截圖
結語
這是我們第一次嘗試用 VPython 畫出向量場的示意圖,看起來效果還不錯,之後應該可以用類似的方法畫出載流導線或線圈產生的磁場示意圖,請參考〈電流的磁效應〉。
太陽系天體資料來源
- 太陽 https://nssdc.gsfc.nasa.gov/planetary/factsheet/sunfact.html
- 水星 https://nssdc.gsfc.nasa.gov/planetary/factsheet/mercuryfact.html
- 金星 https://nssdc.gsfc.nasa.gov/planetary/factsheet/venusfact.html
- 地球 https://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html
- 火星 https://nssdc.gsfc.nasa.gov/planetary/factsheet/marsfact.html
VPython官方說明書
- canvas: http://www.glowscript.org/docs/VPythonDocs/canvas.html
- sphere: http://www.glowscript.org/docs/VPythonDocs/sphere.html
- arrow: http://www.glowscript.org/docs/VPythonDocs/arrow.html
補充 for 迴圈的使用方法
2020/6/10:3 層 for 迴圈
2021/12/23:for 迴圈基本語法
1 層 for 迴圈
若程式碼為
執行時會輸出
range是用來產生數列的函式,格式為
其中起始值預設為0,增量預設為1,執行時不包含結束值。
若程式碼為
執行時會輸出
若程式碼為
執行時會輸出
2 層 for 迴圈
若程式碼為
執行時會輸出
如果想要印出九九乘法表可以這樣寫
執行時會輸出
3 層 for 迴圈
若程式碼為
執行時會輸出
i = 0, j = 0, k = 0
i = 0, j = 0, k = 1
i = 0, j = 0, k = 2
i = 0, j = 0, k = 3
i = 0, j = 1, k = 0
i = 0, j = 1, k = 1
i = 0, j = 1, k = 2
i = 0, j = 1, k = 3
i = 0, j = 2, k = 0
i = 0, j = 2, k = 1
i = 0, j = 2, k = 2
i = 0, j = 2, k = 3
i = 0, j = 3, k = 0
i = 0, j = 3, k = 1
i = 0, j = 3, k = 2
i = 0, j = 3, k = 3
i = 1, j = 0, k = 0
i = 1, j = 0, k = 1
i = 1, j = 0, k = 2
i = 1, j = 0, k = 3
i = 1, j = 1, k = 0
i = 1, j = 1, k = 1
i = 1, j = 1, k = 2
i = 1, j = 1, k = 3
i = 1, j = 2, k = 0
i = 1, j = 2, k = 1
i = 1, j = 2, k = 2
i = 1, j = 2, k = 3
i = 1, j = 3, k = 0
i = 1, j = 3, k = 1
i = 1, j = 3, k = 2
i = 1, j = 3, k = 3
i = 2, j = 0, k = 0
i = 2, j = 0, k = 1
i = 2, j = 0, k = 2
i = 2, j = 0, k = 3
i = 2, j = 1, k = 0
i = 2, j = 1, k = 1
i = 2, j = 1, k = 2
i = 2, j = 1, k = 3
i = 2, j = 2, k = 0
i = 2, j = 2, k = 1
i = 2, j = 2, k = 2
i = 2, j = 2, k = 3
i = 2, j = 3, k = 0
i = 2, j = 3, k = 1
i = 2, j = 3, k = 2
i = 2, j = 3, k = 3
i = 3, j = 0, k = 0
i = 3, j = 0, k = 1
i = 3, j = 0, k = 2
i = 3, j = 0, k = 3
i = 3, j = 1, k = 0
i = 3, j = 1, k = 1
i = 3, j = 1, k = 2
i = 3, j = 1, k = 3
i = 3, j = 2, k = 0
i = 3, j = 2, k = 1
i = 3, j = 2, k = 2
i = 3, j = 2, k = 3
i = 3, j = 3, k = 0
i = 3, j = 3, k = 1
i = 3, j = 3, k = 2
i = 3, j = 3, k = 3
counts = 64