简体   繁体   中英

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. 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:

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'

Here is the curve documentation:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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