# include # include # include # include "logistic_exact.h" /******************************************************************************/ void logistic_exact ( int n, double t[], double *y ) /******************************************************************************/ /* Purpose: logistic_exact() evaluates the exact solution for the logistic ODE. Discussion: Evaluating the exact solution requires the Lambert W function. Licensing: This code is distributed under the MIT license. Modified: 26 May 2024 Author: John Burkardt Input: int n: the number of times. double t[n]: the times. Output: double y[n]: the exact solution values. */ { int i; double k; double r; double t0; double y0; /* Retrieve current parameter values. */ logistic_parameters ( NULL, NULL, NULL, NULL, NULL, &r, &k, &t0, &y0, NULL ); for ( i = 0; i < n; i++ ) { y[i] = ( k * y0 * exp ( r * ( t[i] - t0 ) ) ) / ( k + y0 * ( exp ( r * ( t[i] - t0 ) ) - 1.0 ) ); } return; } /******************************************************************************/ void logistic_parameters ( double *r_in, double *k_in, double *t0_in, double *y0_in, double *tstop_in, double *r_out, double *k_out, double *t0_out, double *y0_out, double *tstop_out ) /******************************************************************************/ /* Purpose: logistic_parameters() returns parameters for logistic_ode(). Discussion: If input values are specified, this resets the default parameters. Otherwise, the output will be the current defaults. Licensing: This code is distributed under the MIT license. Modified: 26 May 2024 Author: John Burkardt Input: double *r_in: the reproduction rate. double *k_in: the carrying capacity. double *t0_in: the initial time. double *y0_in: the initial condition at time T0. double *tstop_in: the final time. Output: double *r_out: the reproduction rate. double *k_out: the carrying capacity. double *t0_out: the initial time. double *y0_out: the initial condition at time T0. double *tstop_out: the final time. */ { static double r_default = 1.0; static double k_default = 1.0; static double t0_default = 0.0; static double y0_default = 0.5; static double tstop_default = 8.0; /* New values, if supplied on input, overwrite the current values. */ if ( r_in ) { r_default = *r_in; } if ( k_in ) { k_default = *k_in; } if ( t0_in ) { t0_default = *t0_in; } if ( y0_in ) { y0_default = *y0_in; } if ( tstop_in ) { tstop_default = *tstop_in; } /* The current values are copied to the output. */ if ( r_out ) { *r_out = r_default; } if ( k_out ) { *k_out = k_default; } if ( t0_out ) { *t0_out = t0_default; } if ( y0_out ) { *y0_out = y0_default; } if ( tstop_out ) { *tstop_out = tstop_default; } return; }