## EX 3: Solving ODEs

### Py 200

#### Write a program that solves the ordinary differential equation, ODE.  You may choose to do either a simple harmonic oscillator or a predator/prey model.

Don't panic. We will work up to this exercise slowly by solving a number of ODE's in class starting with the coffee cooling problem. You may also want to review the mass on a spring section in your introductory physics book.

See the lecture notes for a discussion of the numerical methods that can be used to solve these types of problems.

### Oscillator Problem: Simplest form

Solve the differential equation for a pendulum with a small displacement. (This problem is identical to a mass on a spring.) There are two variables, q, and t corresponding to angle and time. If we assume that the length, mass of the pendulum, and acceleration of gravity are 1 (in some odd units!) then the ODE for this system is:

d / d t ( d q / d t ) = -q

The angle, q, should of course oscillate. This equation is Newton's second law for this sytem.

In order to find a computer solution it is useful to break this equation up into three first order equations. This can be done if you define a new variable, w, corresponding to the angular frequency. The equations for the pendulum can now be written as:

 d w / d t = - q d q / d t = w d t / d t = 1

Prove to yourself that these three equations are equivalent to the simple second order differnetial equation given above.

Be sure and add the ability to start the system with initial values, w 0 and r0, when you code your program

### Problem: Non-linear restoring force

Solve the differential equation for a pendulum without the small angle approximation. There are three variables, q, w, and t corresponding to angle, angular frequency and time. If we assume that the length, mass of the pendulum, and acceleration of gravity are 1 (in some odd units!) then the ODEs for this system are:

 d w / d t = - Sin( q ) d q / d t = w d t / d t = 1

What should happen if the initial position of q is Pi?

### Problem: With friction

Solve the differential equation for a pendulum without the small angle approximation. If we assume that the length and mass of the pendulum are 1 and a frictional force proportional to the angular frequency w, then the ODEs for this system are:

 d w / d t = - Sin( q ) - w * Q d q / d t = w d t / d t = 1

The coefficient of friction is called Q in this program. It should take on a value from 0 to about 2.

### Problem: With friction and a driving force added

Solve the differential equation for a pendulum without the small angle approximation. If we assume that the length and mass of the pendulum are 1 and a frictional force proportional to the angular frequency w, then the ODEs for this system are:

 d w / d t = - Sin( q ) - w*Q + A Sin(2 t / 3 ) d q / d t = w d t / d t = 1

I have choosen a fixed driving frequency of 2/3 Rad/Sec to keep things simple. The amplitude should vary from 0 to about 2 for interesting results.

### Finishing Touch:

Your program can show many different types of graphs. One graph should be the position of the pendulum in radians vs time. Angular velocity vs time is another possibility. An interesting graph might be the position of the pendulum in radians vs the angular velocity of the pendulum. This is called a phase space plot.

## How to Attack the Programming Exercise.

==================================================================

Start with easy changes to the program and only make one change at a time.

### EASY

• Create a user interface that plots a sin curve when you press a hot key.
• Create an input screen that allows you to enter the initial angle, angular velocity and time step.

### MODERATE

• Solve the equations using the Euler method described in class. Just work with the theta vs time graph.
• Add the Omega vs Time Graph.