# include # include # include # include # include # include # include "midpoint_adaptive.h" int main ( ); void lotka_dydt ( double t, double y[], double dy[] ); void lotka_test ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: midpoint_adaptive_test() tests midpoint_adaptive(). Licensing: This code is distributed under the MIT license. Modified: 15 July 2024 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "midpoint_adaptive_test():\n" ); printf ( " C version\n" ); printf ( " midpoint_adaptive() solves ODE's using an adaptive\n" ); printf ( " version of the midpoint method.\n" ); lotka_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "midpoint_adaptive_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return ( 0 ); } /******************************************************************************/ void lotka_test ( ) /******************************************************************************/ /* Purpose: lotka_test() solves lotka_ode() using midpoint_adaptive(). Licensing: This code is distributed under the MIT license. Modified: 11 July 2024 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. */ { double abstol; char label[] = "lotka"; int m; int n_fsolve; int n_rejected; int nstep; int nmax; double reltol; double *t; double t0; double tau0; double tmax; double *y; double y0[2]; printf ( "\n" ); printf ( "lotka_test():\n" ); printf ( " A pair of ordinary differential equations for a population\n" ); printf ( " of predators and prey are solved using midpoint().\n" ); printf ( "\n" ); printf ( " Steady state at r = 5000, f = 2000.\n" ); t0 = 0.0; tmax = 20.0; m = 2; y0[0] = 5000.0; y0[1] = 100.0; tau0 = 2.5E-2; reltol = 1.0E-3; abstol = 1.0E-3; nmax = 400; nstep = 0; t = ( double * ) malloc ( ( nmax + 1 ) * sizeof ( double ) ); y = ( double * ) malloc ( ( nmax + 1 ) * m * sizeof ( double ) ); mad_compute ( lotka_dydt, t0, tmax, m, y0, tau0, reltol, abstol, nmax, &nstep, &n_rejected, &n_fsolve, t, y ); mad_phase_plot ( nstep, m, t, y, label ); mad_solution_plot ( nstep, m, t, y, label ); mad_timestep_plot ( nstep, t, label ); mad_stats ( nstep, n_rejected, n_fsolve, t ); /* Free memory. */ free ( y ); free ( t ); return; } /******************************************************************************/ void lotka_dydt ( double t, double y[], double dydt[] ) /******************************************************************************/ /* Purpose: lotka_dydt() evaluates the right hand side of lotka_ode(). Licensing: This code is distributed under the MIT license. Modified: 27 April 2020 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 t: the current time. double y[2]: the current solution variables, rabbits and foxes. Output: double dydt[2]: the right hand side of the 2 ODE's. */ { dydt[0] = 2.0 * y[0] - 0.001 * y[0] * y[1]; dydt[1] = - 10.0 * y[1] + 0.002 * y[0] * y[1]; return; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 17 June 2014 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }