Sergio Charles
Sergio Charles

Reputation: 379

(Python) TypeError: only integer scalar arrays can be converted to a scalar index

When I compile the following program, Python throws a TypeError: only integer scalar arrays can be converted to a scalar index.

This seems like a relatively straightforward program execution, but I can't seem to resolve it.

Frequency

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()

Let M == sum from j==1 to n of the masses m_j. I expect the program to simply display a plot of period where period(n) is simply defined by the sum from 1 to n of sqrt(g * m_j/(l_j * M)).

Upvotes: 0

Views: 851

Answers (1)

Mortz
Mortz

Reputation: 4879

Use list comprehension to apply period function to each entry in the x array like this -

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

Also, you need to initialize both dem and g -

dem = 0.0
g = 9.8

Upvotes: 1

Related Questions