## Learning and Applying Mathematics using Computing

### Integrated Knowledge

This post is about acceleration, speed and distance, or: a little calculus.

In our last post, we dealt with speed and acceleration. There is a strong relation between these two things, and distance:

• Distance: the amount travelled over time.
• Speed: the rate at which distance increases over time.
• Acceleration: the rate at which speed increases over time.

It can often be useful to change between the different levels on this scale. Moving downwards (distance->speed->acceleration) can be done with differentiation and moving upwards (acceleration->speed->distance) can be done with integration. Together these two things make up most of calculus. Calculus has achieved a reputation of being one of the most difficult areas of maths, but this post will try to convince you otherwise, by looking at the principles of integration. (Technical note: integration operates on a continuous scale, and what I’m doing here is actually summation of discrete series, but it’s close enough to explain the gist.)

## Basic Integration

Our example last time had an apple which accelerated downwards under gravity. The acceleration was 0.4 each frame. The speed to begin with (the 0th frame, if you like) was 0. Each frame, we added on the acceleration, so on the first frame the speed was 0.4, on the second frame it was 0.8, on the third frame it was 1.2, on the fourth frame it was 1.6 and so on. It doesn’t take a genius to see that if we call the frame number $t$ (for time), the speed on any given frame is $0.4 \times t$.

That’s the principle behind the simplest form of integration: if acceleration is some constant $\text{acc}$, then the speed after a given time $t$ is $\text{speedAt(t)} = \text{startingSpeed} + \text{acc} \times t$. Note that this is not specific to acceleration: if, instead, speed is some constant $\text{speed}$, then distance after a given time $t$ is $\text{distanceAt(t)} = \text{startingDistance} + \text{speed} \times t$. This generalises to any other situation: if you have something that changes at a constant rate (read that carefully!), the value at any given time is the starting value, plus the constant change multiplied by the given time.

The complications in integration come when you inevitably need to integrate something more complicated than a constant. Let’s return to our example of having acceleration 0.4, which means a speed of $0.4 \times t$ (0.4, 0.8, 1.2, 1.6 etc as the frames progress). So the distance travelled on the 0th frame is 0, the distance is 0.4 on the first frame, then 1.2 (0.4+0.8), then 2.4 (1.2+1.2), then 4.0 (2.4+1.6). To avoid making this blog post humongous, I’m going to skip the explanation, and just tell you that the pattern here is that if speed is $\text{acc} \times t$ (where $acc$ is constant), then distance travelled is $0.5 \times \text{acc} \times t \times (t + 1)$ (so for the apple: $0.2 \times t \times (t+1)$). You can verify this by feeding in values of $t$ and checked it against the distance values I mentioned earlier in the paragraph. (Technical note: this is the rule for a discrete series, the rule for continous data is different.)

## Meet You at the Bottom

Let’s look at an example of actually using this. Did you notice in the previous post with the apples, that the apples started at the same place, and arrived at the bottom at the same time? I wanted that to happen, so that you could get a good idea of the different falling behaviours, without either apple having travelled faster than the other. I got the apples to arrive at the same time, not by guess-work or trial and error, but by using these calculations.

The green apple on the left-hand side was easy. Both apples started 500 pixels above the bottom of the world, so they had 500 pixels to travel. The green apple fell at 10 pixels per frame, so it would take $\displaystyle\frac{500}{10}=50$ frames to reach the bottom.

The red apple was accelerating at a rate that I could then choose. We’ve already seen that a constant acceleration means that distance travelled is $0.5 \times \text{acc} \times t \times (t + 1)$. We know that $t$ here must be 50, because we are interested in the distance travelled after 50 frames (when the green apple will hit the bottom), and we know that we want the red apple to have travelled 500 pixels after 50 frames, to match the green apple, giving us this equation: $0.5 \times \text{acc} \times 50 \times (50 + 1) = 500$

Working through a couple of steps: $0.5 \times \text{acc} \times 2550 = 500$ $\text{acc} \times 1275 = 500$ $\text{acc} = \frac{500}{1275}$

The right hand side comes out to 0.392 (to 3dp), but I decided that 0.4 was close enough for our purposes. So by making the red apple accelerate at 0.4, I made it reach the bottom of the screen at about the same time as the green apple.