Now you will use a computer program to calculate numerically the behavior of this model of the vibrating string, since it is just a collection of objects (two of which are held fixed, at either end) moving under the influence of calculable forces (from the Hooke’s-law springs).
How many dynamical variables will you need to update during each integration step? What are they?
Each mass feels forces from the two springs that connect it to the two adjacent masses. Write an expression for the net force on mass in terms of the model parameters and the positions of the other masses. (It may be cleaner if you do this in several steps, defining intermediate variables for the separation magnitudes involved. It may also make your code cleaner to introduce such an intermediate variable; remember, memory is cheap!)
As a reminder: Hooke’s law says that a spring with endpoints at and exerts a force on the endpoint at equal to
where is a vector pointing from A to B.
1) You’ll need to store the dynamical variables in arrays. If you are
using C, remember that arrays are passed to and “returned from”
functions in a different way than single numbers. However, it may be
quite helpful to write functions with names like get_forces()
and evolve_euler_cromer()
.
2) Remember that the Euler-Cromer prescription is to update the
positions based on the velocities, then compute the acceleration from
the new positions and update the velocities. However, you need to update allthe
positions first, then all the
velocities. Otherwise you’ll be in a situation where a given update uses
old data for the left neighbor and new data for the right neighbor, or
vice-versa, and you’ll get results that depend on which way you run your for
loop
(which is clearly not right).
3) There is a famous riddle: “A farmer wants to build a
hundred-foot-long fence with fenceposts every ten feet. How many
fenceposts does she need?” The answer is not ten, but eleven; answering
“ten” here is called a “fencepost error” in computing. Note that you have springs
and masses,
but that the first and last of these do not move. This, combined with
the fact that your programming language probably uses zero-indexed
arrays (so an array of 100 things is numbered 0 to 99) means that you
will need to think clearly about the bounds of your for
loops.