【问题标题】:(Python) TypeError: only integer scalar arrays can be converted to a scalar index(Python) TypeError: 只有整数标量数组可以转换为标量索引
【发布时间】:2019-01-11 15:18:29
【问题描述】:

当我编译以下程序时,Python 会抛出 TypeError: only integer scalar arrays can be convert to a scalar index.

这似乎是一个相对简单的程序执行,但我似乎无法解决它。

频率

import matplotlib.pyplot as plt
import numpy as np

def period(n):
    #masses
    m = [1] * n
    #lengths
    l = [2] * n
    M = sum(m)
    num = 2 * math.pi * n

for i in range(n):
    dem = dem + math.sqrt(g * m[i]/(l[i] * M)) 
return num/dem   

x = np.arange(1, 10,1)
y = period(x)
plt.plot(x,y)
plt.show()

M == sum from j==1 to n of the masses m_j。我希望该程序仅显示period 的图,其中period(n) 仅由1nsqrt(g * m_j/(l_j * M)) 之和定义。

【问题讨论】:

    标签: arrays python-3.x list physics


    【解决方案1】:

    使用列表推导将period 函数应用于x 数组中的每个条目,如下所示 -

    y = np.array([period(i) for i in x])
    

    另外,您需要同时初始化demg -

    dem = 0.0
    g = 9.8
    

    【讨论】:

      猜你喜欢
      • 2019-04-26
      • 2021-01-27
      • 2017-12-15
      • 2021-10-31
      • 2018-04-04
      • 2022-01-03
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多