Two masses connected by spring with error: setting an array element with a sequence.

huangapple go评论60阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月10日 21:37:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定