Part 2: Coding

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 N+1 objects (two of which are held fixed, at either end) moving under the influence of calculable forces (from the Hooke’s-law springs).

As a reminder: Hooke’s law says that a spring with endpoints at rA and rB exerts a force on the endpoint at rA equal to

(5)FBA=k(rABr0)r^AB

where rAB 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 N springs and N+1 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.