Maybe you know I like numerical calculations, well I do. I think they are swell. [VPython](http://vpython.org) is my tool of choice. In the post [Basics: Numerical Calculations](http://blog.dotphys.net/2008/10/basics-numerical-calculations/) I used vpython and excel to do something simple. I will do that again today (in that this problem could also be solved analytically). However, there is one big difference. This problem has a non-constant forces. Suppose I have a mass that is connected by a spring to a wall. This mass-spring is sitting on a table with no friction.
There is a very interesting property of springs. The more you stretch them, the greater the force they exert (in the usual model of springs). This model works very well.
This is known as Hooke’s law. I have written it as a scalar for simplicity. The “k” is called the spring constant. It is a measure of how “stiff” the spring is. The value “s” is the amount the spring is stretched. Typically, there is a minus sign in front of the ks to indicate that the force is in the opposite direction that the spring is stretched. Really, in a scalar equation this is rather silly to include (but everyone does anyway).
**Question: What will the motion of the mass be like if I pull it back and then let go?**
Although this can be determined analytically, I am going to first calculate this with vpython. I will try to show all the details so that you can reproduce this also. If you have not already installed [vpython](http://vpython.org), do that now (don’t cost nothing).
In the IDLE editor, enter the following:
- The first line (it’s two underscores, then future, then two more underscores) imports a better division than the standard python division.
- The next line imports all the vpython stuff. This lets you use functions like “sphere(), and vector()”.
- The last import makes it so it is easy to create a graph.
Now you should save and run your program just to make sure everything is ok. If it is fine, you will get a yellow sphere.
Ok, I trust it worked. Here is the plan for the program.
- Set up constants and set up stuff for the graph
- Make a loop
- In the loop, update the force (force of the spring depends on position)
- Use the force to update the momentum
- Update the position using the change in time and the momentum divided by the mass
- Update the graph
- Do the loop again….and again….
Note that I am only representing the ball, not the floor or wall or spring. Also, I am going to set up a weird spring. This spring is attached at the origin and as a zero natural length. This means that any displacement of the mass from the origin will result in a force proportional to that displacement. This is not realistic, but easy to calculate and it gets the point across.
So, let me get started with the setup part:
- I calculated the initial momentum as the mass times the vector(0,0,0) which is STILL the zero vector. However, this way I could easily go back and put the initial velocity as something else.
- ks is the spring constant
- I set the time interval to be small. If you have this too large, things don’t work too well. It IS possible to fix this by changing the “recipe” a little, but I want it to look simple.
- posgraph sets up the graph that I will make
If you want, you can run the program (it won’t do anything) to see if you have any syntax errors.
It works? Ok, here is the rest of the program.
- First thing, I changed the position of the mass to pos=(.5, 0, 0). If I didn’t do this, it would never move because there would be no force to cause a change in momentum. Of course, it would work if I left it at the origin and gave it an initial momentum – try that.
- The fnet vector is just the opposite of the position from the origin of the mass (times some constant). If you want, you can make your mass go in any direction
- posgraph.plot puts a point on the graph. It plots the point at pos=(horizontal value, vertical value). In this case, I want time on the horizontal axis and just the x-component of the position of the mass.
Now run your program. It will happen fast because computers are fast (there is a way to slow down the motion, but I am too impatient for that). Hopefully, your program will produce a graph that looks like this:
This is probably more awesome than you realize. What function does this graph look like? If you had to guess? Does it kind of look like the cosine function? Why is that awesome?
**What is cosine**
The cosine function is simply the ratio of the sides a and c in the diagram below of a right triangle.
Cosine is can also be explained as the projection of a radius of a circle onto one of the axes.
As the red line moves around the circle, the length of the green line changes. The ratio of the green line to the red line is the cosine of the angle of the red line. So, the sine and cosine functions are associated with triangles and circle (and ? – sorry, I just wanted to give a shout out to ?). Well, who cares. Obviously I care. I care because here in the program, do you see a circle? No. Do you see a triangle? No. Do you see ?? No. But, the program clearly produces a cosine function. I think that is awesome.
Well, maybe the program is wrong. No, it’s not wrong. You can easily set up an experiment and get similar data. Also, I can do this problem analytically and get the same answer.
For this solution, I will already assume that all motion takes place in the x-direction. So, I can write Newton’s second law as:
Where Fx is the total force (there is only one in this case anyway). Please forgive me, but I am going to drop the “in the x-direction” notation since EVERYTHING is only in the x-direction. The force from the spring is:
Remember that this spring has zero natural length, so the x position IS the “stretch”. Putting this together, I get:
Here I wrote acceleration as the second time derivative of position. If that is something completely foreign to you, don’t worry – you will see this later maybe. Anyway, what I have here is a differential equation. How do you solve a differential equation? There are lots of strategies, but I find the best one is to guess. Yes, just guess a solution and see if it works. First, let me re-write the differential equation:
If you look at this equation, it says “take the derivative with respect to time twice and get something times the original function” (really, it says that, you might have to listen closely). Once function that does that is ….cosine. So, let me try the function:
Where A and ? are constants. Let me take the first derivative:
And now the second derivative:
So this means that:
Well, does this agree with the numerical solution? One easy way to compare is the period. The period is:
Using the values from the original situation:
Is this what the numerical calculation gives also? I will add the following inside the while loop:
This should print the time if the position of the mass is near where it started. The output is:
So from this, the period is right around 1.256 seconds. They agree. I think this is an excellent example of how numerical calculations are really the same thing as analytical calculations. Ok, they are not the same, but they do the same thing.
2 thoughts on “Spring Motion and Numerical Calculations”
I wrote a similar program last semester to illustrate a mass hanging from a spring fixed at the top end. I wonder if you might show less of your program so that students with a similar assignment aren’t able to simply copy your version. That is the teacher in me thinking. Why are you too impatient to slow down the motion? It takes one really short line at the beginning of your loop.
Sorry, I just had to tweak you on that. I really enjoy your blog and I have linked to you from my school website. I don’t know if any of my students have checked your blog out yet, but I hope they do! Thanks again for the great writing!
Actually, I don’t think of it as a weird spring at all. I just think of it as an elastic medium, with forces due to a displacement field, and the simplest example is the simple harmonic oscillator. I just did this for my classes last week, showing how springs can be done using variable substitution to get the force in terms of displacement out of the integral.
Plus, I never really put this together until I started teaching, but position is the less general quantity, a particular type of displacement, displacement from the origin. If you can grok displacement, it makes it a lot easier to avoid getting confused.