Lab 2 - Lynxes and hares, part I: 9/11

You should have a partner for this lab; they will be arranged in class.

You job in this lab is to write a Java application to simulate the population of lynxes and hares using the Lotka-Volterra equations presented in class.

Introduction:

As discussed in class, the Lotka-Voltera equations are a simple, mathematical model of a predator/prey system.
dH = a*H - alpha*HP
dP = beta*H*P - b*P
These equations have 2 variables (H, and P) and 4 constants (a, b, alpha, and, beta). H is the number of herbavores, P, the number of predators, a is the birth rate for herbavores, b the death rate for predators, alpha is the proportion of Hs eaten by Ps, and beta is the proportion of eaten herbavores that are converted to new predators. dH means the change in H (in one time step), dP, the change in P.

These were originally differential equations, but in this program we will treat them as difference equations.

Empirical findings show that the populations of hares and lynx (in the Hudson Bay region, in the late 1800's) tend to oscillate, with the predator changes following the prey. I.e. after a rise in H, there is a rise in P, then a fall in H followed by a fall in P; then a rise in H, and the cycle continues. Your task is to implement a simulation of these equations, and then, discover values for the initial populations of hares and lynxes, plus the four constants, that produce behavior in the model that is something like the real system. I.e. with oscillations of both populations.

Prototype

First, write an application that has no predators; i.e. P=0. That only leaves one equation:

dH = aH

Thus, you will only need one variable (H) and one constant (a). Here's how to declare those (with initial values of 1000 and 0.01):
        int H=1000;
        double a = 0.01;
    
These declarations should be inside the class {}s, but outside any method {}s (so they will be visible everywhere in the class).

Add a button, which, whenever you press it, computes and prints the number of herbavores at the next time step. A nice way to do this is to write a takeStep() method, that might look like this:
    void takeStep() {
        double dH = a * H;

        H += dH;

        output();
    }

    
And output() looks like:
    void output() {
        System.out.println("H="+ H);
    }
 
Then, you only need to add one line in the actionPerformed() method for the Button (remember, Netbeans writes the shell of the method for you):
    private void stepButtonActionPerformed(java.awt.event.ActionEvent evt) {
        takeStep();
    }
 
Run your program for a few hundred steps and see how those bunnies multiply!

Repeating in Java

It is a little dull to press the button over and over, but it is simple to make things repeat in Java. To make something(); happen 100 times you can write:
        for (int i=0; i<100; i++) {
            something();
        }

 
That's all it takes.
Add another button that will take 100 steps instead of 1.

The whole simulation

Now that you have a working prototype, modify it so it simulates both LV equations.

Try to find initial values for H and P and for the four constants, that yield at least a few oscillations. To do this, add two TextFields, one to enter the initial herbavore population, another to enter the initial predator population (then you won't have to recompile every time you want to try new starting H and P values).

How to get credit: demo with your partner in lab on Thurs; arrange to come in with your programming partner to do your demo.