[英]Solving the VIBRATNIG STRING simulation oproblem in VPYTHON/ python
我是 python 的真正初學者,現在我被下面的代碼困住了,我不知道如何繼續前進。 它沒有向我顯示我正在嘗試編碼的曲線。 這個想法是它將顯示兩個球(它們正在工作)和它們之間的弦,它們將像振動的吉他弦一樣以慢動作移動。
請給我一些建議
from vpython import *
import math
rho = 0.01
ten = 40.
c = math.sqrt(ten / rho) # 4000
c1 = c
ratio = c * c / (c1 * c1)
steps = 101
x = list(range(0, 100))
y = list(range(0, 100))
g = canvas(width=600, height=400, title="Vibrating string")
string = curve(x, y, color=color.yellow, radius=0.5, x0=steps * [0.], x1=steps * [0.], x2=steps * [0.])
ball1 = sphere(pos=vector(50, 0, 0), radius=2.0, color=color.red)
ball2 = sphere(pos=vector(-50, 0, 0), radius=2.0, color=color.red)
for i in range(0, 81):
string.x0[i] = 0.00125 * i
for i in range(81, steps):
string.x0[i] = 0.1 - 0.005 * (i - 80)
for i in range(0, 100):
string.x[i] = 2.0 * i - 100.0
string.y[i] = 300.0 * string.x0[i]
string.pos = vector(string.x[i], string.y[i], 0)
for i in range(1, 100):
string.x1[i] = string.x0[i] + 0.5 * ratio * (string.x0[i + 1] + string.x0[i + 1] - 2 * string.x0[i])
string.x0[i] = string.x1[i]
while 1:
rate(50)
for i in range(1, 100):
string.x2[i] = 2. * string.x1[i] - string.x0[i] + ratio * (
string.x1[i + 1] + string.x1[i - 1] - 2 * string.x1[i])
string.modify(i, y=string.x1[i])
for i in range(0, 100):
string.x[i] = 2.0 * i - 100.0
string.y[i] = 300.0 * string.x2[i]
string.pos = vector(string.x[i], string.y[i], 0)
for i in range(0, 101):
string.x0[i] = string.x1[i]
string.x1[i] = string.x2[i]
曲線 object 沒有屬性“x”和“y”,這就是程序給出此錯誤消息的原因:
回溯(最近一次調用):文件“D:\0C\workspace\glowscript\test.py”,第 24 行,在 string.x[i] = 2.0 * i - 100.0 AttributeError: 'curve' object has no attribute ' X'
這是曲線文檔:
https://www.glowscript.org/docs/VPythonDocs/curve.html
一個小問題:您不需要導入數學。 當你說“from vpython import *”時,數學模塊會自動導入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.