英文:
Two masses connected by spring with error: setting an array element with a sequence
问题
我正在编写一个用Python模拟两个由弹簧连接的质点的代码。然而,不知何故,我总是遇到"ValueError: setting an array element with a sequence"错误。有人能帮助我找出问题在哪里吗?我已经查看了这个论坛上的其他答案,但没有帮助。以下是你的Python代码:
import numpy as np
k = 1.95
m1 = 0.1
m2 = 0.3
x10 = 0.03
x20 = 0.01
v10 = 0
v20 = 0
t0 = 0
tf = 25
dt = 0.05
x0 = 5
k1_drag = 0.2
t = np.arange(t0, tf, dt)
x1 = np.zeros_like(t)
x2 = np.zeros_like(t)
v1 = np.zeros_like(t)
v2 = np.zeros_like(t)
a1 = np.zeros_like(t)
a2 = np.zeros_like(t)
def F1(x1, x2, x0 = 5):
return k*(x2 - x1 -x0) - (k1_drag/m1) * v1
def F2(x1, x2, x0 = 5):
return -k*(x2 - x1 - x0) - (k1_drag/m2) * v2
x1[0] = x10
x2[0] = x20
v1[0] = v10
v2[0] = v20
t[0] = t0
for i in range(1, len(t)):
a1[i-1] = F1(x1[i-1], x2[i-1])
a2[i-1] = F2(x1[i-1], x2[i-1])
v1[i] = v1[i-1] + a1[i-1]*dt
v2[i] = v2[i-1] + a2[i-1]*dt
x1[i] = x1[i-1] + v1[i-1]*dt
x2[i] = x2[i-1] + v2[i-1]*dt
t[i] = t[i-1] + dt
英文:
I am working on a code simulating two masses connected by a spring in Python. However, somehow I always get the ValueError: setting an array element with a sequence. Can someone help me figuring out what the problem is here? I have gone through other answers on this forum but it did not help.
import numpy as np
k = 1.95
m1 = 0.1
m2 = 0.3
x10 = 0.03
x20 = 0.01
v10 = 0
v20 = 0
t0 = 0
tf = 25
dt = 0.05
x0 = 5
k1_drag = 0.2
t = np.arange(t0, tf, dt)
x1 = np.zeros_like(t)
x2 = np.zeros_like(t)
v1 = np.zeros_like(t)
v2 = np.zeros_like(t)
a1 = np.zeros_like(t)
a2 = np.zeros_like(t)
def F1(x1, x2, x0 = 5):
return k*(x2 - x1 -x0) - (k1_drag/m1) * v1
def F2(x1, x2, x0 = 5):
return -k*(x2 - x1 - x0) - (k1_drag/m2) * v2
x1[0] = x10
x2[0] = x20
v1[0] = v10
v2[0] = v20
t[0] = t0
for i in range(1, len(t)):
a1[i-1] = F1(x1[i-1], x2[i-1])
a2[i-1] = F2(x1[i-1], x2[i-1])
v1[i] = v1[i-1] + a1[i-1]*dt
v2[i] = v2[i-1] + a2[i-1]*dt
x1[i] = x1[i-1] + v1[i-1]*dt
x2[i] = x2[i-1] + v2[i-1]*dt
t[i] = t[i-1] + dt
答案1
得分: 1
"F1" 返回一个序列,无法赋值给数组元素。
英文:
F1
returns a sequence, which cannot be assigned to array element.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论