I'm trying to simulate a simple pendulum using pylot. For that, I created the class Pendulum and, in one of the methods, I keep getting the same error.
def __init__(self, L = 1, M = 1, G = 9.8, origin = (0, 0), init = [60, 0]):
self.init_state = np.array(init, dtype = 'float')
self.params = (L, M, G)
self.origin = origin
self.time = 0
self.state = self.init_state * (np.pi/180.)
def dstate_dt(self, state, t):
"""compute the derivative of the given state"""
(L, M, G) = self.params
dydx = np.zero_lie(state)
dydx[0] = state[1]
dydx[1] = -(G/L)*np.sin(state[0])
return dydx
The error code reads:
File "C:\Users\Lucas\Anaconda3\envs\Spyder\lib\site-packages\numpy\core\fromnumeric.py", line 47, in _wrapit result = getattr(asarray(obj), method)(*args, **kwds)
TypeError: only integer scalar arrays can be converted to a scalar index
Going into the refered line, in a folder called 'fromnumeric.pi', the given function reads:
def _wrapit(obj, method, *args, **kwds):
try:
wrap = obj.__array_wrap__
except AttributeError:
wrap = None
result = getattr(asarray(obj), method)(*args, **kwds)
if wrap:
if not isinstance(result, mu.ndarray):
result = asarray(result)
result = wrap(result)
return result
What triggered this error and how can I fix it? The full traceback is this:
File "C:\Users\Lucas\Desktop\Estudos\Python\Simple Pendulum.py", line 86, in animate(0)
File "C:\Users\Lucas\Desktop\Estudos\Python\Simple Pendulum.py", line 80, in animate line.set_data(pendulum.position())
File "C:\Users\Lucas\Desktop\Estudos\Python\Simple Pendulum.py", line 27, in position x = np.cumsum(self.origin[0], L*np.sin(self.state[0]))
File "<array_function internals>", line 6, in cumsum
File "C:\Users\Lucas\Anaconda3\envs\Spyder\lib\site-packages\numpy\core\fromnumeric.py", line 2423, in cumsum return _wrapfunc(a, 'cumsum', axis=axis, dtype=dtype, out=out)
File "C:\Users\Lucas\Anaconda3\envs\Spyder\lib\site-packages\numpy\core\fromnumeric.py", line 58, in _wrapfunc return _wrapit(obj, method, *args, **kwds)
File "C:\Users\Lucas\Anaconda3\envs\Spyder\lib\site-packages\numpy\core\fromnumeric.py", line 47, in _wrapit result = getattr(asarray(obj), method)(*args, **kwds)
TypeError: only integer scalar arrays can be converted to a scalar index
np.zero_lieshould benp.zeros_like- Darina