This chapter describes functions for solving ordinary differential equation (ODE) initial value problems.  The library provides a variety of low-level methods, such as Runge-Kutta and Bulirsch-Stoer routines, and higher-level components for adaptive step-size control.  The components can be combined by the user to achieve the desired solution, with full access to any intermediate steps.

These functions are declared in the header file gsl_odeiv.h.

Defining the ODE System
=======================

The routines solve the general n-dimensional first-order system,

     dy_i(t)/dt = f_i(t, y_1(t), ..., y_n(t))

for i = 1, \dots, n.  The stepping functions rely on the vector of derivatives f_i and the Jacobian matrix, $J_ij = \partial f_i(t, y(t)) / \partial y_j$ J_{ij} = df_i(t,y(t)) / dy_j. A system of equations is defined using the gsl_odeiv_system datatype.

 -- Data Type: gsl_odeiv_system
     This data type defines a general ODE system with arbitrary parameters.

     int (* function) (double t, const double y[], double dydt[], void * params)
          This function should store the vector elements $f_i(t,y,\hbox\it params)$ f_i(t,y,params) in the array DYDT, for arguments (T,Y) and parameters PARAMS. The function should return GSL_SUCCESS if the calculation was completed successfully. Any other return value indicates an error.

     int (* jacobian) (double t, const double y[], double * dfdy, double dfdt[], void * params);
          This function should store the vector of derivative elements $\partial f_i(t,y,params) / \partial t$ df_i(t,y,params)/dt in the array DFDT and the Jacobian matrix $J_ij$ J_{ij} in the array DFDY, regarded as a row-ordered matrix J(i,j) = dfdy[i * dimension + j] where dimension is the dimension of the system. The function should return GSL_SUCCESS if the calculation was completed successfully.  Any other return value indicates an error.

          Some of the simpler solver algorithms do not make use of the Jacobian matrix, so it is not always strictly necessary to provide it (the jacobian element of the struct can be replaced by a null pointer for those algorithms).  However, it is useful to provide the Jacobian to allow the solver algorithms to be interchanged---the best algorithms make use of the Jacobian.

     size_t dimension;
          This is the dimension of the system of equations.

     void * params
          This is a pointer to the arbitrary parameters of the system.

Stepping Functions
==================

The lowest level components are the stepping functions which advance a solution from time t to t+h for a fixed step-size h and estimate the resulting local error.

 -- Function: gsl_odeiv_step * gsl_odeiv_step_alloc (const gsl_odeiv_step_type * T, size_t DIM)
     This function returns a pointer to a newly allocated instance of a stepping function of type T for a system of DIM dimensions.

 -- Function: int gsl_odeiv_step_reset (gsl_odeiv_step * S)
     This function resets the stepping function S.  It should be used whenever the next use of S will not be a continuation of a previous step.

 -- Function: void gsl_odeiv_step_free (gsl_odeiv_step * S)
     This function frees all the memory associated with the stepping function S.

 -- Function: const char * gsl_odeiv_step_name (const gsl_odeiv_step * S)
     This function returns a pointer to the name of the stepping function. For example,

          printf ("step method is '%s'\n",
                   gsl_odeiv_step_name (s));

     would print something like step method is 'rk4'.

 -- Function: unsigned int gsl_odeiv_step_order (const gsl_odeiv_step * S)
     This function returns the order of the stepping function on the previous step.  This order can vary if the stepping function itself is adaptive.

 -- Function: int gsl_odeiv_step_apply (gsl_odeiv_step * S, double T, double H, double Y[], double YERR[], const double DYDT_IN[], double DYDT_OUT[], const gsl_odeiv_system * DYDT)
     This function applies the stepping function S to the system of equations defined by DYDT, using the step size H to advance the system from time T and state Y to time T+H. The new state of the system is stored in Y on output, with an estimate of the absolute error in each component stored in YERR. If the argument DYDT_IN is not null it should point an array containing the derivatives for the system at time T on input. This is optional as the derivatives will be computed internally if they are not provided, but allows the reuse of existing derivative information. On output the new derivatives of the system at time T+H will be stored in DYDT_OUT if it is not null.

     If the user-supplied functions defined in the system DYDT return a status other than GSL_SUCCESS the step will be aborted.  In this case, the elements of Y will be restored to their pre-step values and the error code from the user-supplied function will be returned. The step-size H will be set to the step-size which caused the error. If the function is called again with a smaller step-size, e.g. H/10, it should be possible to get closer to any singularity. To distinguish between error codes from the user-supplied functions and those from gsl_odeiv_step_apply itself, any user-defined return values should be distinct from the standard GSL error codes.

The following algorithms are available,

 -- Step Type: gsl_odeiv_step_rk2
     Embedded Runge-Kutta (2, 3) method.

 -- Step Type: gsl_odeiv_step_rk4
     4th order (classical) Runge-Kutta.

 -- Step Type: gsl_odeiv_step_rkf45
     Embedded Runge-Kutta-Fehlberg (4, 5) method.  This method is a good general-purpose integrator.

 -- Step Type: gsl_odeiv_step_rkck
     Embedded Runge-Kutta Cash-Karp (4, 5) method.

 -- Step Type: gsl_odeiv_step_rk8pd
     Embedded Runge-Kutta Prince-Dormand (8,9) method.

 -- Step Type: gsl_odeiv_step_rk2imp
     Implicit 2nd order Runge-Kutta at Gaussian points.

 -- Step Type: gsl_odeiv_step_rk4imp
     Implicit 4th order Runge-Kutta at Gaussian points.

 -- Step Type: gsl_odeiv_step_bsimp
     Implicit Bulirsch-Stoer method of Bader and Deuflhard.  This algorithm requires the Jacobian.

 -- Step Type: gsl_odeiv_step_gear1
     M=1 implicit Gear method.

 -- Step Type: gsl_odeiv_step_gear2
     M=2 implicit Gear method.

Adaptive Step-size Control
==========================

The control function examines the proposed change to the solution produced by a stepping function and attempts to determine the optimal step-size for a user-specified level of error.

 -- Function: gsl_odeiv_control * gsl_odeiv_control_standard_new (double EPS_ABS, double EPS_REL, double A_Y, double A_DYDT)
     The standard control object is a four parameter heuristic based on absolute and relative errors EPS_ABS and EPS_REL, and scaling factors A_Y and A_DYDT for the system state y(t) and derivatives y'(t) respectively.

     The step-size adjustment procedure for this method begins by computing the desired error level D_i for each component,

          D_i = eps_abs + eps_rel * (a_y |y_i| + a_dydt h |y'_i|)

     and comparing it with the observed error E_i = |yerr_i|.  If the observed error E exceeds the desired error level D by more than 10% for any component then the method reduces the step-size by an appropriate factor,

          h_new = h_old * S * (E/D)^(-1/q)

     where q is the consistency order of the method (e.g. q=4 for 4(5) embedded RK), and S is a safety factor of 0.9. The ratio E/D is taken to be the maximum of the ratios E_i/D_i.

     If the observed error E is less than 50% of the desired error level D for the maximum ratio E_i/D_i then the algorithm takes the opportunity to increase the step-size to bring the error in line with the desired level,

          h_new = h_old * S * (E/D)^(-1/(q+1))

     This encompasses all the standard error scaling methods. To avoid uncontrolled changes in the stepsize, the overall scaling factor is limited to the range 1/5 to 5.

 -- Function: gsl_odeiv_control * gsl_odeiv_control_y_new (double EPS_ABS, double EPS_REL)
     This function creates a new control object which will keep the local error on each step within an absolute error of EPS_ABS and relative error of EPS_REL with respect to the solution y_i(t). This is equivalent to the standard control object with A_Y=1 and A_DYDT=0.

 -- Function: gsl_odeiv_control * gsl_odeiv_control_yp_new (double EPS_ABS, double EPS_REL)
     This function creates a new control object which will keep the local error on each step within an absolute error of EPS_ABS and relative error of EPS_REL with respect to the derivatives of the solution y'_i(t).  This is equivalent to the standard control object with A_Y=0 and A_DYDT=1.

 -- Function: gsl_odeiv_control * gsl_odeiv_control_scaled_new (double EPS_ABS, double EPS_REL, double A_Y, double A_DYDT, const double SCALE_ABS[], size_t DIM)
     This function creates a new control object which uses the same algorithm as gsl_odeiv_control_standard_new but with an absolute error which is scaled for each component by the array SCALE_ABS. The formula for D_i for this control object is,

          D_i = eps_abs * s_i + eps_rel * (a_y |y_i| + a_dydt h |y'_i|)

     where s_i is the i-th component of the array SCALE_ABS. The same error control heuristic is used by the Matlab ODE suite.

 -- Function: gsl_odeiv_control * gsl_odeiv_control_alloc (const gsl_odeiv_control_type * T)
     This function returns a pointer to a newly allocated instance of a control function of type T.  This function is only needed for defining new types of control functions.  For most purposes the standard control functions described above should be sufficient.

 -- Function: int gsl_odeiv_control_init (gsl_odeiv_control * C, double EPS_ABS, double EPS_REL, double A_Y, double A_DYDT)
     This function initializes the control function C with the parameters EPS_ABS (absolute error), EPS_REL (relative error), A_Y (scaling factor for y) and A_DYDT (scaling factor for derivatives).

 -- Function: void gsl_odeiv_control_free (gsl_odeiv_control * C)
     This function frees all the memory associated with the control function C.

 -- Function: int gsl_odeiv_control_hadjust (gsl_odeiv_control * C, gsl_odeiv_step * S, const double Y[], const double YERR[], const double DYDT[], double * H)
     This function adjusts the step-size H using the control function C, and the current values of Y, YERR and DYDT. The stepping function STEP is also needed to determine the order of the method.  If the error in the y-values YERR is found to be too large then the step-size H is reduced and the function returns GSL_ODEIV_HADJ_DEC.  If the error is sufficiently small then H may be increased and GSL_ODEIV_HADJ_INC is returned.  The function returns GSL_ODEIV_HADJ_NIL if the step-size is unchanged.  The goal of the function is to estimate the largest step-size which satisfies the user-specified accuracy requirements for the current point.

 -- Function: const char * gsl_odeiv_control_name (const gsl_odeiv_control * C)
     This function returns a pointer to the name of the control function. For example,

          printf ("control method is '%s'\n",
                  gsl_odeiv_control_name (c));

     would print something like control method is 'standard'

Evolution
=========

The highest level of the system is the evolution function which combines the results of a stepping function and control function to reliably advance the solution forward over an interval (t_0, t_1).  If the control function signals that the step-size should be decreased the evolution function backs out of the current step and tries the proposed smaller step-size.  This process is continued until an acceptable step-size is found.

 -- Function: gsl_odeiv_evolve * gsl_odeiv_evolve_alloc (size_t DIM)
     This function returns a pointer to a newly allocated instance of an evolution function for a system of DIM dimensions.

 -- Function: int gsl_odeiv_evolve_apply (gsl_odeiv_evolve * E, gsl_odeiv_control * CON, gsl_odeiv_step * STEP, const gsl_odeiv_system * DYDT, double * T, double T1, double * H, double Y[])
     This function advances the system (E, DYDT) from time T and position Y using the stepping function STEP. The new time and position are stored in T and Y on output. The initial step-size is taken as H, but this will be modified using the control function C to achieve the appropriate error bound if necessary.  The routine may make several calls to STEP in order to determine the optimum step-size. An estimate of the local error for the step can be obtained from the components of the array E->yerr[]. If the step-size has been changed the value of H will be modified on output. The maximum time T1 is guaranteed not to be exceeded by the time-step.  On the final time-step the value of T will be set to T1 exactly.

     If the user-supplied functions defined in the system DYDT return a status other than GSL_SUCCESS the step will be aborted.  In this case,  T and Y will be restored to their pre-step values and the error code from the user-supplied function will be returned.  To distinguish between error codes from the user-supplied functions and those from gsl_odeiv_evolve_apply itself, any user-defined return values should be distinct from the standard GSL error codes.

 -- Function: int gsl_odeiv_evolve_reset (gsl_odeiv_evolve * E)
     This function resets the evolution function E.  It should be used whenever the next use of E will not be a continuation of a previous step.

 -- Function: void gsl_odeiv_evolve_free (gsl_odeiv_evolve * E)
     This function frees all the memory associated with the evolution function E.

Examples
========

The following program solves the second-order nonlinear Van der Pol oscillator equation,

     x''(t) + \mu x'(t) (x(t)^2 - 1) + x(t) = 0

This can be converted into a first order system suitable for use with the routines described in this chapter by introducing a separate variable for the velocity, y = x'(t),

     x' = y
     y' = -x + \mu y (1-x^2)

The program begins by defining functions for these derivatives and their Jacobian,

          [file ode-initval.c not included]

For functions with multiple parameters, the appropriate information can be passed in through the PARAMS argument using a pointer to a struct.

The main loop of the program evolves the solution from (y, y') = (1, 0) at t=0 to t=100.  The step-size h is automatically adjusted by the controller to maintain an absolute accuracy of $10^-6$ 10^{-6} in the function values Y.

To obtain the values at user-specified positions, rather than those chosen automatically by the control function, the main loop can be modified to advance the solution from one chosen point to the next.  For example, the following main loop prints the solution at the points t_i = 0, 1, 2, \dots, 100,

       for (i = 1; i <= 100; i++)
         {
           double ti = i * t1 / 100.0;

           while (t < ti)
             {
               gsl_odeiv_evolve_apply (e, c, s,
                                       &sys,
                                       &t, ti, &h,
                                       y);
             }

           printf ("%.5e %.5e %.5e\n", t, y[0], y[1]);
         }

Note that arbitrary values of t_i can be used for each stage of the integration.  The equally spaced points in this example are just used as an illustration.

It is also possible to work with a non-adaptive integrator, using only the stepping function itself.  The following program uses the rk4 fourth-order Runge-Kutta stepping function with a fixed stepsize of 0.01,

          [file odefixed.c not included]

The derivatives must be initialized for the starting point t=0 before the first step is taken.  Subsequent steps use the output derivatives DYDT_OUT as inputs to the next step by copying their values into DYDT_IN.

References and Further Reading
==============================

Many of the basic Runge-Kutta formulas can be found in the Handbook of Mathematical Functions,

  Abramowitz & Stegun (eds.), Handbook of Mathematical Functions, Section 25.5.

The implicit Bulirsch-Stoer algorithm bsimp is described in the following paper,

  G. Bader and P. Deuflhard, ``A Semi-Implicit Mid-Point Rule for Stiff Systems of Ordinary Differential Equations.'', Numer. Math. 41, 373--398, 1983.