Function as argument raises TypeError ndarray

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

Function as argument raises TypeError ndarray

问题

我必须分析一个具有可变加速度的质量。第一个函数定义了加速度。第二个函数分别返回在时间t处的位置、速度和加速度的数组(t从0到10,间隔为0.1)。

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. dt = 0.1
  4. t0 = 0.0
  5. t1 = 10.0
  6. x0 = 0.0
  7. v0 = 0.0
  8. m = 5.0
  9. t = np.linspace(0, 10, 101)
  10. def versnelling(t):
  11. return (0.7 * np.sin(3 * t)) / m
  12. def numeriek(x0, v0, a_func, t):
  13. x = np.zeros(len(t))
  14. v = np.zeros(len(t))
  15. a = np.zeros(len(t))
  16. x[0] = x0
  17. v[0] = v0
  18. a[0] = a_func(t[0])
  19. for i in range(len(t) - 1):
  20. dt = t[i + 1] - t[i]
  21. a[i + 1] = a0 + a_func(i)
  22. v[i + 1] = v[i] + a[i] * dt
  23. x[i + 1] = x[i] + v[i] * dt
  24. return x, v, a

但是当我调用它时:

  1. numeriek(x0, v0, versnelling(t), t)

我得到:

  1. 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)

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. dt = 0.1
  4. t0 = 0.0
  5. t1 = 10.0
  6. x0 = 0.0
  7. v0 = 0.0
  8. m = 5.0
  9. t = np.linspace(0, 10, 101)
  10. def versnelling(t):
  11. return (0.7 * np.sin(3 * t)) / m
  12. def numeriek(x0, v0, a_func, t):
  13. x = np.zeros(len(t))
  14. v = np.zeros(len(t))
  15. a = np.zeros(len(t))
  16. x[0] = x0
  17. v[0] = v0
  18. a[0] = a_func(t[0])
  19. for i in range(len(t) - 1):
  20. dt = t[i + 1] - t[i]
  21. a[i + 1] = a0 + a_func(i)
  22. v[i + 1] = v[i] + a[i] * dt
  23. x[i + 1] = x[i] + v[i] * dt
  24. return x, v, a

But when I call it:

  1. numeriek(x0, v0, versnelling(t), t)

I get:

  1. 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),后者实际上是一个函数调用,返回一个值。

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. dt = 0.1
  4. t0 = 0.0
  5. t1 = 10.0
  6. x0 = 0.0
  7. v0 = 0.0
  8. m = 5.0
  9. t = np.linspace(0, 10, 101)
  10. def versnelling(t):
  11. return (0.7 * np.sin(3 * t)) / m
  12. def numeriek(x0, v0, a_func, t):
  13. x = np.zeros(len(t))
  14. v = np.zeros(len(t))
  15. a = np.zeros(len(t))
  16. x[0] = x0
  17. v[0] = v0
  18. a[0] = a_func(t[0])
  19. for i in range(len(t) - 1):
  20. dt = t[i + 1] - t[i]
  21. a[i + 1] = a[0] + a_func(i)
  22. v[i + 1] = v[i] + a[i] * dt
  23. x[i + 1] = x[i] + v[i] * dt
  24. return x, v, a
  25. if __name__ == "__main__":
  26. 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.

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. dt = 0.1
  4. t0 = 0.0
  5. t1 = 10.0
  6. x0 = 0.0
  7. v0 = 0.0
  8. m = 5.0
  9. t = np.linspace(0, 10, 101)
  10. def versnelling(t):
  11. return (0.7 * np.sin(3 * t)) / m
  12. def numeriek(x0, v0, a_func, t):
  13. x = np.zeros(len(t))
  14. v = np.zeros(len(t))
  15. a = np.zeros(len(t))
  16. x[0] = x0
  17. v[0] = v0
  18. a[0] = a_func(t[0])
  19. for i in range(len(t) - 1):
  20. dt = t[i + 1] - t[i]
  21. a[i + 1] = a[0] + a_func(i)
  22. v[i + 1] = v[i] + a[i] * dt
  23. x[i + 1] = x[i] + v[i] * dt
  24. return x, v, a
  25. if __name__ == "__main__":
  26. print(numeriek(x0, v0, versnelling, t))

huangapple
  • 本文由 发表于 2023年2月16日 03:15:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464492.html
匿名

发表评论

匿名网友

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

确定