Here's How to Solve the xkcd Velociraptor Problem With Code

This classic xkcd physics problem involves an accelerating velociraptor. Here is a numerical solution to this problem using python.
VelociraptorWyomingDinosaurCenterWhiteBackground
Wyoming Dinosaur Center

Every once in a while, I think about velociraptors. Often the first thing that comes to mind is this xkcd comic with 3 velociraptor homework questions. The first question says:

The velociraptor spots you 40 meters away and attacks, accelerating at 4 m/s^2 up to its top speed of 25 m/s. When it spots you, you begin to flee, quickly reaching your top speed of 6 m/s. How far can you get before you're caught and devoured?

As with all great problems, it's a great problem because a student is motivated to know the answer. Wait...why would you want to know when you will be devoured? Maybe it's not the best question. Well, I'm going to solve it anyway.

For this problem, I am going to solve it using some python code. Why am I using python? I'm using it because this is a great time to show you how awesome python can be. Is it cheating to use python instead of just creating a solution using a bit of algebra and a piece of paper? I don't think so. Both the numerical method (using python) and analytical method (with algebra) can give valid solutions. Both have their advantages and disadvantages. Since many people might already be familiar with an analytical solution, I am going to focus on the numerical.

Before we start, let's write down everything we know.

  • I will put the human at the origin on the x-axis. The velociraptor starts 40 meters away at x = -40 m.
  • Both the human and velociraptor start with an x-velocity of 0 m/s (this is a one dimensional problem so that everything is in the x-direction).
  • The velociraptor will accelerate at 4 m/s2 until reaching a speed of 25 m/s. I will assume during the accelerating phase, the velociraptor has a constant acceleration.
  • The problem doesn't say anything about the human's acceleration. I guess you could say the human just instantaneously starts with a speed of 6 m/s. For my calculation, I am going to say the human has an acceleration of 3 m/s2 (just for fun).
  • One final assumption. I will assume that the velociraptor has to be in the exact position as the human in order to devour.

Now for the strategy. First, let's look at acceleration in 1 dimension. For constant acceleration, I can define it as:

La te xi t 1

This says that if I know the starting velocity, acceleration, and the length of the time interval then I can find the final velocity.

La te xi t 1

Even though an object is accelerating, we can make an approximation that it is moving at a constant velocity. This approximation isn't too terrible if the time interval is small. With this, I can write a similar expression for the position.

La te xi t 1

Yes, this is only true if the velocity is constant (or if we use the average velocity). However, if the time interval is very small then the average velocity is the same as the initial (or final) velocity. Trust me, this works.

So here is the plan. I will break the motion of the human and the velociraptor into tiny time steps. During each step I will do the following.

  • Check if either the human or velociraptor is at maximum speed. If they are, then change their acceleration to zero.
  • Calculate the velocity of the two objects would have at the end of this tiny time interval.
  • Use this velocity to calculate the new position of the two objects.
  • Increase the time by the amount of the time interval.

Just keep repeating those steps until the position of the velociraptor is the same as the position as the human.

Now for the solution. Here is a bit of code that I created to solve this problem. Take a look at it and even try changing things (trust me, you can't break anything).

Homework. Yes, I am giving homework to go along with this solution. Here is your homework:

1. In the above problem, the velociraptor will always devour the human. It's only a question of "where". Create a different situation with a velociraptor and a human in which there is a solution that does not end with the death of the human.

Oh, your answer to the above homework question should probably also have a solution to go with it. And yes, I will also be posting a solution to the xckd velociraptor problem number 2.