Traject.java

/**
A 2D ballistic trajectory with drag.
*/
public class Traject implements ODE
{
    private double state[] = new double[5];
    private double c = 0.0;
    private double gee = 1.0; // good enough

    /**
    A 2D ballistic trajectory with drag.  Assumes unit mass of
    projectile and unit g force.

    @param v 'muzzle' velocity
    @param elev elevation of muzzle (in radians)
    @param c drag coefficient
    */
    public Traject(double v,double elev,double c)
    {
        this.c = c; // drag coefficient
        state[0] = 0.0; // time
        state[1] = 0.0; // x
        state[2] = 0.0; // y
        state[3] = v*Math.cos(elev); // vx
        state[4] = v*Math.sin(elev); // vy
    }

    /**
    Return state vector for the trajectory: time, x, y, vx, vy.
    */
    public double[] getState()
    {
        return state;
    }

    /**
    Compute and return time derivatives in <code>rate</code> 
    (1, vx, vy, ax, ay) based on state vector in <code>state</code>
    (t, x, y, vx, vy).
    */
    public void getRate(double[] state,double[] rate)
    {
        // Assume unit mass and unit gee.
        double v = Math.sqrt(
            Math.pow(state[3],2.0) + Math.pow(state[4],2.0));
        rate[0] = 1.0;
        rate[1] = state[3]; // x
        rate[2] = state[4]; // y
        rate[3] = -c*v*state[3]; // vx
        rate[4] = -c*v*state[4] - gee; // vy
    }
}