英文:
Function as argument raises TypeError ndarray
问题
我必须分析一个具有可变加速度的质量。第一个函数定义了加速度。第二个函数分别返回在时间t处的位置、速度和加速度的数组(t从0到10,间隔为0.1)。
import numpy as np
import matplotlib.pyplot as plt
dt = 0.1
t0 = 0.0
t1 = 10.0
x0 = 0.0
v0 = 0.0
m = 5.0
t = np.linspace(0, 10, 101)
def versnelling(t):
return (0.7 * np.sin(3 * t)) / m
def numeriek(x0, v0, a_func, t):
x = np.zeros(len(t))
v = np.zeros(len(t))
a = np.zeros(len(t))
x[0] = x0
v[0] = v0
a[0] = a_func(t[0])
for i in range(len(t) - 1):
dt = t[i + 1] - t[i]
a[i + 1] = a0 + a_func(i)
v[i + 1] = v[i] + a[i] * dt
x[i + 1] = x[i] + v[i] * dt
return x, v, a
但是当我调用它时:
numeriek(x0, v0, versnelling(t), t)
我得到:
TypeError: 'numpy.ndarray' object is not callable
我尝试在 a_function()
中使用一个 0,以及一个 t
。
我应该如何修复它,但更重要的是为什么我的代码不起作用?
英文:
I have to analyse a mass with variable acceleration. The first function defines acceleration. The second return arrays for respectively place, speed and acceleration at time t. (t goes from 0-10 with increments of 0.1)
import numpy as np
import matplotlib.pyplot as plt
dt = 0.1
t0 = 0.0
t1 = 10.0
x0 = 0.0
v0 = 0.0
m = 5.0
t = np.linspace(0, 10, 101)
def versnelling(t):
return (0.7 * np.sin(3 * t)) / m
def numeriek(x0, v0, a_func, t):
x = np.zeros(len(t))
v = np.zeros(len(t))
a = np.zeros(len(t))
x[0] = x0
v[0] = v0
a[0] = a_func(t[0])
for i in range(len(t) - 1):
dt = t[i + 1] - t[i]
a[i + 1] = a0 + a_func(i)
v[i + 1] = v[i] + a[i] * dt
x[i + 1] = x[i] + v[i] * dt
return x, v, a
But when I call it:
numeriek(x0, v0, versnelling(t), t)
I get:
TypeError: 'numpy.ndarray' object is not callable
I tried just a 0 in a_function()
, as well as a t
How do I fix it but most of all why does my code not work??
答案1
得分: 1
参数 a_func
似乎是一个函数,所以当你调用 numeriek
时,应该传递它作为 versnelling
,而不是 versnelling(t)
,后者实际上是一个函数调用,返回一个值。
import numpy as np
import matplotlib.pyplot as plt
dt = 0.1
t0 = 0.0
t1 = 10.0
x0 = 0.0
v0 = 0.0
m = 5.0
t = np.linspace(0, 10, 101)
def versnelling(t):
return (0.7 * np.sin(3 * t)) / m
def numeriek(x0, v0, a_func, t):
x = np.zeros(len(t))
v = np.zeros(len(t))
a = np.zeros(len(t))
x[0] = x0
v[0] = v0
a[0] = a_func(t[0])
for i in range(len(t) - 1):
dt = t[i + 1] - t[i]
a[i + 1] = a[0] + a_func(i)
v[i + 1] = v[i] + a[i] * dt
x[i + 1] = x[i] + v[i] * dt
return x, v, a
if __name__ == "__main__":
print(numeriek(x0, v0, versnelling, t))
英文:
The parameter a_func
seems to be a function, so when you call the numeriek
it should be passed as versnelling
and not versnelling(t)
that is in fact a function invocation resulting in a value.
import numpy as np
import matplotlib.pyplot as plt
dt = 0.1
t0 = 0.0
t1 = 10.0
x0 = 0.0
v0 = 0.0
m = 5.0
t = np.linspace(0, 10, 101)
def versnelling(t):
return (0.7 * np.sin(3 * t)) / m
def numeriek(x0, v0, a_func, t):
x = np.zeros(len(t))
v = np.zeros(len(t))
a = np.zeros(len(t))
x[0] = x0
v[0] = v0
a[0] = a_func(t[0])
for i in range(len(t) - 1):
dt = t[i + 1] - t[i]
a[i + 1] = a[0] + a_func(i)
v[i + 1] = v[i] + a[i] * dt
x[i + 1] = x[i] + v[i] * dt
return x, v, a
if __name__ == "__main__":
print(numeriek(x0, v0, versnelling, t))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论