/* nag_ode_ivp_rkts_errass (d02puc) Example Program. * * Copyright 2013 Numerical Algorithms Group. * * Mark 24, 2013. */ #include #include #include #include #ifdef __cplusplus extern "C" { #endif static void NAG_CALL f(double t, Integer n, const double *y, double *yp, Nag_Comm *comm); #ifdef __cplusplus } #endif #define N 4 int main(void) { /* Scalars */ Integer exit_status = 0; Integer liwsav, lrwsav, lwcomm, n; double errmax, hnext, hstart, tend, terrmx, tgot, tol, tstart, twant, waste; Integer fevals, j, k, stepcost, stepsok; /* Arrays */ static double ruser[1] = {-1.0}; double *rmserr = 0, *rwsav = 0, *thresh = 0, *wcomm = 0; double *ygot = 0, *yinit = 0, *ymax = 0, *ypgot = 0; Integer *iwsav = 0; char nag_enum_arg[40]; /* NAG types */ NagError fail; Nag_RK_method method; Nag_ErrorAssess errass; Nag_Comm comm; INIT_FAIL(fail); n = N; liwsav = 130; lrwsav = 350 + 32 * n; lwcomm = 6*n; printf("nag_ode_ivp_rkts_errass (d02puc) Example Program Results\n\n"); /* For communication with user-supplied functions: */ comm.user = ruser; if ( !(rmserr = NAG_ALLOC(n, double))|| !(thresh = NAG_ALLOC(n, double))|| !(ygot = NAG_ALLOC(n, double))|| !(yinit = NAG_ALLOC(n, double))|| !(ymax = NAG_ALLOC(n, double))|| !(ypgot = NAG_ALLOC(n, double))|| !(wcomm = NAG_ALLOC(lwcomm, double))|| !(rwsav = NAG_ALLOC(lrwsav, double))|| !(iwsav = NAG_ALLOC(liwsav, Integer)) ) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* Skip heading in data file*/ scanf("%*[^\n] "); /* Set initial conditions for ODE and parameters for the integrator. */ scanf(" %39s%*[^\n] ", nag_enum_arg); /* nag_enum_name_to_value (x04nac) Converts NAG enum member name to value. */ method = (Nag_RK_method) nag_enum_name_to_value(nag_enum_arg); scanf(" %39s%*[^\n] ", nag_enum_arg); errass = (Nag_ErrorAssess) nag_enum_name_to_value(nag_enum_arg); scanf("%lf%lf%*[^\n] ", &tstart, &tend); for (j = 0; j < n; j++) scanf("%lf", &yinit[j]); scanf("%*[^\n] "); scanf("%lf%lf%*[^\n] ", &hstart, &tol); for (j = 0; j < n; j++) scanf("%lf", &thresh[j]); scanf("%*[^\n] "); /* Initialize Runge-Kutta method for integrating ODE using * nag_ode_ivp_rkts_setup (d02pqc). */ nag_ode_ivp_rkts_setup(n, tstart, tend, yinit, tol, thresh, method, errass, hstart, iwsav, rwsav, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_ode_ivp_rkts_setup (d02pqc).\n%s\n", fail.message); exit_status = 1; goto END; } printf(" Calculation with tol = %8.1e\n", tol); printf(" t y1 y1'\n"); printf("%6.3f", tstart); for (k = 0; k < n; k++) printf(" %8.4f", yinit[k]); printf("\n"); twant = tend; tgot = tstart; while (tgotuser[0] == -1.0) { printf("(User-supplied callback f, first invocation.)\n"); comm->user[0] = 0.0; } r = sqrt(y[0]*y[0] + y[1]*y[1]); r = r*r*r; yp[0] = y[2]; yp[1] = y[3]; yp[2] = -y[0]/r; yp[3] = -y[1]/r; }