Trying to make a simple kinematics array (for fun!) where

1. You're prompted for an int input tmax that determines t = [0, tmax]

So this would mean that if you input tmax = 5, the time interval would be t = [0, 5] seconds

1. You're prompted for a float input jerkc (third derivative of position) that determines the constant used in calculation of acceleration (jerkc * t), velocity (jerkc * 1/2 * t^2) and position.

I'm trying to use NumPy (I'm very new to this, and to python) to create a two-dimensional array where:

Row 1: time (so for tmax = 5 this row would be: [0, 1, 2, 3, 4, 5])

Row 2: acceleration(t) (so for tmax = 5, Jerkc = 2 this row would be [0, 2, 4, 6, 8, 10]

Row 3: velocity(t) (so on...)

Row 4: position(t) (so on.....)

I'm still very unfamiliar with lists - and especially NumPy arrays. I'm used to Java arrays, if you're wondering why I'm treating this array the way I am. Here's a sample of what I have so far:

``````import numpy as np

tmax = int(input('Please enter a timeframe [0, t] [int]: '))
print('Thank you! Your timeframe will be [0, ', tmax, ']!')
jerkc = float(input('Please enter a jerk constant [float]: '))
print('Thank you! Your jerk constant will be ', jerkc, '!')

physics = np.array([[], [], [], []])

t = int(0)
i = int(0)
m = int(0)
e = int(0)

while (t <= tmax):
physics[0, t] = t
t = t + 1
while (i <= tmax):
physics[1, t] = (jerkc * t)
t = t + 1
while (m <= tmax):
physics[2, t] = ((jerkc) * (1/2) * (t ^ 2))
t = t + 1
while (e <= tmax):
physics[3, t] = ((jerkc) * (1/2) * (1/3) * (t ^ 3))

print(physics)
``````

If anyone knows what I'm doing wrong, or can explain to me a way I can use arrays better, please let me know, and please explain yourself carefully and understandably! Thank you!

🔴 No definitive solution yet
📌 Solution 1

The special thing about NumPy arrays is that you can do calculations with it. This means that the if you multiply 2 arrays, the result array will contain the values of those arrays multiplied with eachother.
As in this example:

``````>>> array1=array([1,2,3,4])
>>> array2=array([5,6,7,8])
>>>
>>> array1 * array2
array([ 5, 12, 21, 32])
``````

This does also work for addition, substraction, division...

Another great thing in NumPy is the `arange` function. It works like the `range` function in python, but instaed returns an array. It has three arguments: start, end and step. The returned array will start with the start argument. The following item will then be added up with the step value, and so on. Untill the next item would be larger then the end value.
Here's an example:

``````>>> arange(1,10)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> arange(1,10,2)
array([1, 3, 5, 7, 9])
>>> arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
``````

Note: When the step argument is not provided, it is defaulted to 1. If you provide only one argument, it will start from 0 and end with this argument.

There's also a `zeros` function. It returns an array of the given size, filled with zeros. It is preferrable to initialise arrays in this way.

``````>>> zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>>> zeros((2,2))
array([[0., 0.],
[0., 0.]])
``````

If we would rewrite your code using the previously mentioned things, it would look like:

``````import numpy as np

tmax = int(input('Please enter a timeframe [0, t] [int]: '))
print('Thank you! Your timeframe will be [0, ', tmax, ']!')
jerkc = float(input('Please enter a jerk constant [float]: '))
print('Thank you! Your jerk constant will be ', jerkc, '!')

physics = np.zeros((4,tmax+1))

physics[0, :] = np.arange(tmax+1)#+1, as the end is not included in the array.
physics[1, :] = physics[0, :] * jerkc
physics[2, :] = ((jerkc) * (1/2) * (physics[0, :] ** 2))
physics[3, :] = ((jerkc) * (1/2) * (1/3) * (physics[0, :] ** 3))
``````

Note: `**` is the power operator in python, `^` is exclusive or and thus won't work.