Example #1
0
    private static void stiff_midpoint_explicit_test(double[] tspan, double[] y0, int n)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    stiff_midpoint_explicit_test() tests stiff_midpoint_explicit().
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    06 April 2021
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Input:
    //
    //    double TSPAN(2): the first and last times.
    //
    //    double Y0: the initial condition.
    //
    //    int N: the number of steps to take.
    //
    {
        const int m  = 1;
        const int n2 = 101;

        Console.WriteLine("");
        Console.WriteLine("stiff_midpoint_explicit_test");
        Console.WriteLine("  Solve stiff ODE using the midpoint_explicit method.");

        double[] t1 = new double[n + 1];
        double[] y1 = new double[n + 1];
        MidpointExplicit.midpoint_explicit(stiff_deriv, tspan, y0, n, m, ref t1, ref y1);

        double[] t2 = typeMethods.r8vec_linspace_new(n2, tspan[0], tspan[1]);
        double[] y2 = stiff_exact(n2, t2);

        plot2(n + 1, t1, y1, n2, t2, y2, "stiff_midpoint_explicit",
              "Stiff ODE: midpoint explicit method");
    }
Example #2
0
    private static void predator_prey_midpoint_explicit_test(double[] tspan, double[] p0, int n)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    predator_prey_midpoint_explicit_test() tests predator_prey_midpoint_explicit().
    //
    //  Discussion:
    //
    //    The physical system under consideration is a pair of animal populations.
    //
    //    The PREY reproduce rapidly for each animal alive at the beginning of the
    //    year, two more will be born by the end of the year.  The prey do not have
    //    a natural death rate instead, they only die by being eaten by the predator.
    //    Every prey animal has 1 chance in 1000 of being eaten in a given year by
    //    a given predator.
    //
    //    The PREDATORS only die of starvation, but this happens very quickly.
    //    If unfed, a predator will tend to starve in about 1/10 of a year.
    //    On the other hand, the predator reproduction rate is dependent on
    //    eating prey, and the chances of this depend on the number of available prey.
    //
    //    The resulting differential equations can be written:
    //
    //      PREY(0) = 5000
    //      PRED(0) =  100
    //
    //      d PREY / dT =    2 * PREY(T) - 0.001 * PREY(T) * PRED(T)
    //      d PRED / dT = - 10 * PRED(T) + 0.002 * PREY(T) * PRED(T)
    //
    //    Here, the initial values (5000,100) are a somewhat arbitrary starting point.
    //
    //    The pair of ordinary differential equations that result have an interesting
    //    behavior.  For certain choices of the interaction coefficients (such as
    //    those given here), the populations of predator and prey will tend to
    //    a periodic oscillation.  The two populations will be out of phase the number
    //    of prey will rise, then after a delay, the predators will rise as the prey
    //    begins to fall, causing the predator population to crash again.
    //
    //    There is a conserved quantity, which here would be:
    //      E(r,f) = 0.002 r + 0.001 f - 10 ln(r) - 2 ln(f)
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    06 April 2021
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    George Lindfield, John Penny,
    //    Numerical Methods Using MATLAB,
    //    Second Edition,
    //    Prentice Hall, 1999,
    //    ISBN: 0-13-012641-1,
    //    LC: QA297.P45.
    //
    //  Input:
    //
    //    double TSPAN = [ T0, TMAX ], the initial and final times.
    //    A reasonable value might be [ 0, 5 ].
    //
    //    double P0 = [ PREY, PRED ], the initial number of prey and predators.
    //    A reasonable value might be [ 5000, 100 ].
    //
    //    int N: the number of time steps.
    //
    {
        List <string> command = new();
        List <string> data    = new();
        const string  header  = "predator_prey_midpoint_explicit";
        int           i;
        const int     m = 2;

        Console.WriteLine("");
        Console.WriteLine("predator_prey_midpoint_test");
        Console.WriteLine("  A pair of ordinary differential equations for a population");
        Console.WriteLine("  of predators and prey are solved using midpoint_explicit().");

        double[] t    = new double[n + 1];
        double[] pout = new double[(n + 1) * m];

        MidpointExplicit.midpoint_explicit(predator_prey_deriv, tspan, p0, n, m, ref t, ref pout);
        //
        //  Create the data file.
        //
        string data_filename = header + "_data.txt";

        for (i = 0; i < n; i++)
        {
            data.Add("  " + t[i]
                     + "  " + pout[0 + i * m]
                     + "  " + pout[1 + i * m] + "");
        }

        File.WriteAllLines(data_filename, data);

        Console.WriteLine("");
        Console.WriteLine("  predator_prey_midpoint_explicit_test: data stored in '" + data_filename + "'.");
        //
        //  Create the command file.
        //
        string command_filename = header + "_commands.txt";

        command.Add("# " + command_filename + "");
        command.Add("#");
        command.Add("# Usage:");
        command.Add("#  gnuplot < " + command_filename + "");
        command.Add("#");
        command.Add("set term png");
        command.Add("set output '" + header + ".png'");
        command.Add("set xlabel '<-- PREDATOR -->'");
        command.Add("set ylabel '<-- PREY -->'");
        command.Add("set title 'Predator prey: midpoint explicit'");
        command.Add("set grid");
        command.Add("set style data lines");
        command.Add("plot '" + data_filename + "' using 2:3 with lines lw 3");
        command.Add("quit");

        File.WriteAllLines(command_filename, command);

        Console.WriteLine("  predator_prey_midpoint_explicit_test: plot commands stored in '" + command_filename +
                          "'.");
    }