Everyone already uses computers in physics. At the very least, students use handheld calculators (I doubt anyone is still using a slide-rule calculator). Also, it's becoming more common to have students solve physics problem by creating and coding their own programs—and I think that is a good thing. If you aren't familiar with these numerical calculations (another name for computational physics), the basic idea is to take a problem and break it into many smaller and simpler problems. These smaller problems are easier to solve, but you get so many calculations that you basically have to write a computer program to complete them (but you technically don't have to use a computer).

But as numerical methods become more common, we also have to discuss the role of these methods in terms of the nature of science. I often see quotes like this: "Computational methods expand our tool set in physics. We now have three parts of science: experiment, theory, and computations."

However, this just isn't true. You can't break science into three different parts. Computational methods and theory are really just two versions of a calculation—and they really aren't that different. I'm going to show you how these are the same, but first let me be clear about the nature of science. Science is all about the building and testing of models. We create models about the way the universe works, and then we test these models with experimental evidence. These models could be an actual physical model (like a globe), a conceptual model, an equation—or even a computer program. So, both "theory" and "computation" are both models.

Let's start with a mass connected to a spring. I'll be honest, we physicists LOVE this situation. It's easy enough to solve but complicated enough that we can approximate many other things as just a mass on a spring. For example, when a block sits on a table, the contact force can be modeled as a spring. Even the interaction between atoms in a solid can be approximated as a spring force. Really, this problem is everywhere. But here it is in its most basic form.

I'm going to solve this problem two ways. First, I will solve it numerically by breaking it into small pieces (and using some Python code). After that, I will find an analytical solution—a solution that is a closed form function (like in terms of cosine) so that you can put in whatever numbers and parameters you want to get a bunch of solutions. But in the end, I'll show you that these two methods aren't really that different.

Numerical Solution

In order to build a numerical model for a mass connected to a spring, we need an expression for the force a spring exerts. If you take a spring and pull it, it pulls back with some force. The more you stretch it, the harder it pulls. Suppose the position of a mass is given by the variable x such that this is also the stretch of the spring. In that case, the spring force (in one dimension) would be:

In this expression, k is a measure of the stiffness of the spring (called the spring constant). The negative sign means that if you pull the spring in the positive x-direction, the force will pull back in the negative direction. OK, so there's a force on the mass. What does a force do to an object? It causes a change in velocity. You can see this with Newton's second law (again in one dimension).

Where the acceleration is defined as the rate of change of velocity and the velocity is the rate of change of position.

But now maybe you can see the problem. The force from the spring changes the velocity of the mass—and the velocity changes the position, which changes the force. This makes it a seem like a circular problem that is impossible to solve (it's not actually impossible). So, the key in the numerical calculation is to break this problem into short time intervals. During each of these time intervals, I can do the following:

- Use the position of the mass to calculate the force.
- Assume this force is constant during this period to find the velocity at the end of the time interval.
- Again assume the velocity is constant to calculate the new position of the mass.
- Repeat these steps for as long as you want or until your computer explodes.

Yes, that's the short version of this calculation. If you want more details, check out this older post.

OK, now we just need to convert these instructions for these calculations from human language into Python (it's really not that difficult). However, it's important to remember that for a numerical calculation you need NUMBERS. Yes, in each step of the process you use the numerical values from the previous time interval to find the new stuff. So, I need some values for the position, velocity, mass, and the spring constant. Here are the arbitrary numbers I'm going to use (you can change these in the code below if it makes you happy).

- Mass = 0.1 kg
- Spring constant = 5 N/m
- Initial velocity = 0 m/s
- Initial position = 0.03 m

I can then just plot the position of the mass at each time interval to get the following graph. Note—this is not just a graph. It's actual code. If you click the "pencil" icon, you can see the program and even change the values.

You can see the mass oscillates back and forth to produce this very nice and smooth graph.

Analytical Solution

OK, now let's solve this problem again—but not using a computer. Warning, there is going to be some math. In particular, I am going to have to take some derivatives. You don't have to fully follow along, just believe that it's true.

I will again start with Newton's second law and the force exerted by a spring.

Instead of defining the acceleration as the rate of change of velocity, I could instead say that it's the derivative of velocity with respect to time. The velocity then is the derivative of the position with respect to time. That means the acceleration is the second derivative of position. Yes, there's going to be some more derivatives. It will be fine.

Yes, I divided both sides by the mass to get the second derivative by itself. Technically, this is a differential equation. But hold on—we can solve this just by thinking about it for a little bit. This equation says that there is some function of x. If you take the derivative of this function twice, you get that same function back with an extra factor of -k/m. What are some functions of which you can take the derivative and get something similar? There are a few that would work, but I'm going to go with the cosine function. Let's guess a solution that looks like this:

In this expression, A and ω are just constants. If I take the first and second derivative, I get this.

In order for this function to work, the constant ω has to be related to the spring constant and mass.

We can get a value for the constant A by knowing that at t = 0 seconds, the x position has to be 0.03 meters (from the example before). But that means A is the amplitude of the cosine function. Boom. We did it. We solved for the position of the oscillating mass. Oh, and check it out. The numerical solution above ALSO looks EXACTLY like a cosine function. Actually, it's pretty awesome that these two methods give the same numerical values.

But they are different, right? I mean, in the numerical method I just get a plot of the cosine function. It's just numbers. For the analytical solution I get a trig function—I don't even have to put in the numbers to understand that the solution oscillates up and down since that's what the cosine function does. Big difference. Or is it?

Let's go back to my original values. What if I want to find out exactly where the mass is after 1.0 seconds (I just picked that number). For the numerical solution, I can just look at the graph and look up the value of the data when t = 1.0 seconds (I get about 0.0201 meters). What about for the analytical solution? Well, first I need to calculate these constants. The value for A will just be 0.03 meters. The constant ω would be 7.07 radians/second. Great. Now I just have to multiply ω by the time of 1 second and take the cosine of that.

Oh no. How do you take the cosine of 7.07? Yes, it's true you can put the value into your calculator and it will give you an answer—but your calculator is essentially a mini computer (literally true in this case). It's actually not so trivial to take the cosine of some function. Let me show you.

Go ahead and grab your calculator. Make sure your calculator is in radians mode and then type in 7.07 and press the "cos" button. Yes, it's going to output something like 0.706104. But how does it get that number? Really, there are two options (I'm not sure which one your calculator uses). The first option is a "look up" table. This is literally just a list of numbers between 0 and 2π with values of cosine. Since cosine is a periodic function, you can take any value greater than 2π and subtract 2π and then use the look-up table.

The second option is … wait for it … a numerical calculation. Yes, you can calculate the value of cosine by using a bunch of simple calculations. One of the older (and most common) methods is called the CORDIC algorithm. But really, there's no other way to calculate the cosine of a value—either look it up in a table or do a numerical calculation.

So, here we are. Back with the model nature of science. It doesn't matter if you use Python or a differential equation to determine the motion of a mass on a spring—it's still a model, and these two methods really aren't that different. It's all numerical whether you like it or not.