You try to index outside the range:
for s in range(0, M+1):
t[s] = t_min + k*s
Change to:
for s in range(M):
t[s] = t_min + k*s
And it works.
You create t
with length of M
:
t = np.linspace(t_min, t_max, M)
So you can only access M
elements in t
.
Python always starts indexing with zero. Therefore:
for s in range(M):
will do M
loops, while:
for s in range(0, M+1):
will do M+1
loops.