/* * The program solves the first-order nonlinear equation, * * y'(t) = u(t)^2 - u(t)^3 * * y[0] = 0.0001, 0 <= t <= 20000 */ #include #include #include #include int func (double t, const double y[], double f[], void *params) { f[0] = y[0]*y[0]*(1. - y[0]); return GSL_SUCCESS; } int main (void) { size_t neqs = 1; /* number of equations */ double eps_abs = 1.e-10, eps_rel = 0.; /* desired precision */ double stepsize = 1e-6; /* initial integration step size */ double t = 0., t1 = 20000.; /* time interval of the evolution */ int status; /* * Initial conditions */ double y[1] = { 0.0001 }; /* * Explicit embedded Runge-Kutta-Fehlberg (4,5) method. * This method is a good general-purpose integrator. */ gsl_odeiv2_step *s = gsl_odeiv2_step_alloc (gsl_odeiv2_step_rkf45, neqs); gsl_odeiv2_control *c = gsl_odeiv2_control_y_new (eps_abs, eps_rel); gsl_odeiv2_evolve *e = gsl_odeiv2_evolve_alloc (neqs); gsl_odeiv2_system sys = {func, NULL, neqs, NULL}; /* * Evolution loop */ while (t < t1) { status = gsl_odeiv2_evolve_apply (e, c, s, &sys, &t, t1, &stepsize, y); if (status != GSL_SUCCESS) { printf ("Troubles at % .5e % .5e \n", t, y[0]); break; } printf ("% .5e % .5e \n", t, y[0]); } gsl_odeiv2_evolve_free (e); gsl_odeiv2_control_free (c); gsl_odeiv2_step_free (s); return 0; }