简体   繁体   English

解决VPYTHON/python中的VIBRATNIG STRING仿真问题

[英]Solving the VIBRATNIG STRING simulation oproblem in VPYTHON/ python

I am a true beginner in python and right now I am stuck with the code below, I can't figured our how to move forward with it.我是 python 的真正初学者,现在我被下面的代码困住了,我不知道如何继续前进。 It doesb't show me a curve which I am trying to code.它没有向我显示我正在尝试编码的曲线。 The idea is that it will show two balls (which are working) and string between them, which will be moving like vibrating string of gituar in slow motion.这个想法是它将显示两个球(它们正在工作)和它们之间的弦,它们将像振动的吉他弦一样以慢动作移动。

Please give me some advices请给我一些建议

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]

The curve object does not have attributes "x" and "y", which is why the program gives this error message:曲线 object 没有属性“x”和“y”,这就是程序给出此错误消息的原因:

Traceback (most recent call last): File "D:\0C\workspace\glowscript\test.py", line 24, in string.x[i] = 2.0 * i - 100.0 AttributeError: 'curve' object has no attribute 'x'回溯(最近一次调用):文件“D:\0C\workspace\glowscript\test.py”,第 24 行,在 string.x[i] = 2.0 * i - 100.0 AttributeError: 'curve' object has no attribute ' X'

Here is the curve documentation:这是曲线文档:

https://www.glowscript.org/docs/VPythonDocs/curve.html https://www.glowscript.org/docs/VPythonDocs/curve.html

A minor point: You don't need to import math.一个小问题:您不需要导入数学。 When you say "from vpython import *", the math module is automatically imported.当你说“from vpython import *”时,数学模块会自动导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM