Numerical Calculations Can Seem Like Magic

Numerical Calculations Can Seem Like Magic

It all starts with a simple mass on hanging on a spring.

Numerical Calculations Can Seem Like Magic


I've written about numerical calculations before, but perhaps they weren't too inspiring.

What are numerical calculations? Here is my own definition.

Numerical Calculations (numerical methods, computational methods): The process of taking a complex problem and breaking it into many smaller and simpler problems. Typically, these many simple problems are calculated using a computer.

Perhaps it is useful to also define analytic calculations (or methods). In this case, a combination of algebra and perhaps calculus is used to arrive at an answer.

Numerical calculations are just a part of the way we do science now. It isn't cheating, it's a legitimate method. If the results of the numerical calculation agree with actual data, then it works. It's that simple.

Very Basic Physics ——————

Sometimes we can use numerical methods to find the trajectory of an object with forces acting on it. Here are the key ideas that we will start with. For simplicity, I am going to assume that everything is moving in just the x-direction. This means that I can use scalar values instead of vectors. If you have no idea what that means, that's ok.

Position and Velocity: Suppose I have some object on the x axis. In this case, I can write down its x-coordinate. Now, what if the object moves to another location in some time interval (Δt)? I can determine the average velocity (the x-velocity) as:

La te xi t 1

Velocity and Acceleration: If the velocity of an object is changing, we can define the average acceleration as:

La te xi t 1

Momentum Principle: This says two things. First, it says that momentum is the product of mass and velocity. Second it says that a net force CHANGES the momentum of an object (in case you can't tell, change is the important part). I can write this momentum principle with the following equations.

La te xi t 1

So, if the mass doesn't change with time then I get the all too familiar "F = ma" expression (but really it is the NET force).

That is all the basic physics you need. Let's move forward.

Building a Numerical Model ————————–

Let's say I have some object with a force on it. I don't want to use any fancy algebra or calculus to find the motion of this object. Instead I want to use a numerical model. Here's what we will do. First, break the motion into tiny time steps. Let's just say each step is 0.01 seconds. During each step, I will do the following:

  • Calculate the force on the object as well as the acceleration.
  • Use the acceleration and the velocity in the previous time step to find the new velocity.
  • Use this new velocity and the old position to find the new position.
  • Update the time.
  • Repeat the above for as long as you want.

But how do you find the new velocity? Suppose in time step 1 I have a velocity v1. If the acceleration is constant, then I can write:

La te xi t 1

That seems straightforward, right? But what if the acceleration isn't constant? What then? Here is the trick. If the time interval is small, it doesn't really matter if the acceleration is constant or not. I know, it seems crazy. But remember: if the model agrees with data then it works. That's it.

Oh, we can do the same thing when we find the new position.

Modeling the Motion of a Mass on a Spring —————————————–

Let's get to the magic. Suppose I have a mass attached to a spring. The cool thing about springs is that the more you stretch them, the greater the force they pull back with. We call this Hooke's Law. If the spring is unstretched at x = 0 meters, then I can write this as:

La te xi t 1

The variable k is called the spring constant. It's just a measure of the stiffness of the spring.

Now suppose I pull a mass off to the side a little bit and then let go. It's not such a simple problem as you might think since the force changes whenever the mass moves.

If I use the numerical recipe from above, I can make a very simple program. In this case, I am using the web-based GlowScrip (it's like VPython but you don't have to install anything).

Here is the program. Let me point out a few things.

Glow Script ide

The numbers are the line numbers in the program.

  • 3: This sets up a graph.
  • 4-10: Just makes some variables.
  • 12-17: This is a loop that goes through the different time steps.
  • 13: Calculate the acceleration based on the spring force.
  • 14: Update the velocity (note that the "=" isn't an algebraic equal sign - instead, it is the "assign" sign).
  • 15: Update the position.
  • 16: Update the time.
  • 17: Update the plot of x vs. t.

The most important thing to see is just how simple this program is. There is nothing hidden here (well, except for the plotting function). So, there is nothing up my sleeve. Now, if I run this SUPER simple program I get the following graph (or you can do it yourself).

Glow Script ide 2

MAGIC. What does that plot look like? Does it look like a cosine function? If you aren't sure, here is the same plot right next to the cosine function (I shifted it up a little bit so they wouldn't be right on top of each other).

Glow Script ide 3

This is freaky awesome. FREAKY! Why? Well, what is the cosine function? Basically this just says that there is a relationship between the sides and angles for a right triangle. Here is such a triangle.

For this triangle, the cosine function would be:

La te xi t 1

If this is a cosine plot, WHERE IS THE TRIANGLE? There isn't a triangle, there isn't a circle but there is still a cosine function. CRAZY. But there's more! It turns out that the blue cosine function above has the form:

La te xi t 1

The time for one complete oscillation of this mass would be:

La te xi t 1

You can actually measure the spring constant, the mass and the time for one oscillation (we call this the period). Then where does that π come from? Tell me? Why is there a π in this expression? It's really there and there isn't even a circle?

Honestly, I'm not sure I have a good explanation for this. Maybe the best I can say is that it is there because it works. I know it works because I can actually plot the motion of a mass on a spring and it looks JUST like this.

I guess numerical calculations are magical. They are, but they are simple too.