Course 1.2Lesson 5 of 5
A project that uses the whole chapter: a toy car is measured at its position each second, and the arrays give its speed and acceleration. np.diff of the positions divided by np.diff of the times gives the speed in each second, and the same step on the speeds gives the acceleration, each at the middle of its interval. Three graphs stacked with sharex show position, speed and acceleration against the same time axis. The slope of the position graph is the speed, and the slope of the speed graph is the acceleration. The car speeds up at 1.6 metres per second squared for about 5 seconds, then cruises at 8 metres per second, and the distance in the last 5 seconds is 40 metres, which equals the speed times the time.
Try the code and the simulations beside the lesson.
After this lesson you can
A toy car drives along a straight track. A camera records its position every second, for ten seconds. The data is a table of times and positions. The questions are: how fast is the car going at each moment, and does it speed up?
In physics, speed is the change of position divided by the change of time, and acceleration is the change of speed divided by the change of time. With arrays, these are two lines of code: you need the difference between each number and the next one. That is what np.diff does.
Predict first. The position goes 0, 0.8, 3.2, 7.2, ... What is the speed in the first second? What do you expect for the last second, when the positions go up by 8 each time? Then run it.
Now the three graphs. plt.subplots(3, 1, figsize=(6, 7), sharex=True) makes three axes one above the other that share the same time axis, so that a moment on one graph is directly above the same moment on the others.
Each speed belongs to the middle of its one-second step, which is why the dots sit at half seconds.
The slope links the graphs. The slope of the position graph at any moment is the speed at that moment. The slope of the speed graph is the acceleration. When you read a graph, look at the slope.
Look at the acceleration between 4 and 6 seconds: it comes down through 0.8. The car did not really accelerate at 0.8 for any time. The reason is that the change from accelerating to cruising happened between two measurements, in the middle of one second. Our method gives the average over that second. A camera that takes more pictures per second shows the sharper change. This is a normal limit of calculating from measured data: the answer has the same resolution as the data.
x[-1] - x[5]. The two ways agree.np.diff(x) by the time itself and not by np.diff(t).ValueError. Use the middle times.np.diff makes the array one shorter.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
np.diff(x) / np.diff(t). Acceleration is the same step on the speeds.plt.subplots(3, 1, sharex=True) stacks graphs that share a time axis.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.