Assignment #4


For this assignment you will do a little book work and a little programming. First, do the following questions from chapter6.

 

Now for the coding aspect of the assignment. For this assignment I want you to write a general purpose 4th order Runga-Kutta integrator and use it to solve two different problems. The first problem you will apply it to is the coupled oscillator problem. This is basically a system with two springs and two masses where one spring connects a mass to a fixed point and the other spring connects that mass to the other mass. You can assume that there is no damping/friction in the system. The second problem will be a general gravity simulation where you have bodies in space that attract one another through a gravitational force given by Newton's Law of Gravitation.

First lets look more closely at the coupled pendulum. You have two masses, m1 and m2 that are connected with two springs with constants k1 and k2. The masses are at positions x1 and x2. We assume that x1 is measured relative to the natural resting position of the first spring as attached to the wall where positive x1 means it has been pulled further from the wall. Similarly, we assume that x2 is measured relative to the natural resting position of the second mass if neither spring is tretched or compressed at all. (If you need a picture of this, ask in class.) So the stretching of the first spring is given by x1 and the stretching of the second spring is given by x2-x1. Each mass has a velocity, vx1 and vx2. So the force on mass 1 is given by F1=-k1*x1+k2*(x2-x1) and the force on mass 2 is given by F2=-k2*(x2-x1). As a hint, you want a state vector with 4 elements in it, x1, x2, vx1, and vx2.

When you run this simulation, you will get a data file with the following format x1 x2 vx1 vx2 m1 m2 k1 k2. You will output a data file called "spring.txt" that has three columns, t, x1, and x2.

The second system you will do has a number of bodies moving under the force of gravity. Each each body and a mass as well as a position and velocity in 3 space (x, y, z, vx, vy, vz). Newton's law of gravity says that force between two bodies if F=-GMm/(r^2), where M is the mass of one body, m is the mass of the other, r is the distance between them, and G is the Universal constant of gravitation. To keep things simple, you can assume you are always given things in units where G=1. Of course, the force has a direction along the line between the points. To help you out, we can break the formula into parts. Fx=-Mmx/(r^3), Fy=-Mmy/(r^3), and Fz=-Mmz/(r^3), where x, y, and z are the separations between the bodies in each direction. Note that the forcing is between a pair of bodies and the force on one is the opposite of the force on the other.

When you run this simulation, you will get a data file with the following format. It starts with a single line giving yo the number of particles. That is followed by one line for each particle where each line has x, y, z, vx, vy, vz, mass. You will output a data file called "grav.txt" that has 3*n+1 columns, t, x1, y1, x2, y2, ..., xn, yn.

I want your program to ask the user which type of simulation they want to do, then ask for the name of the input file.