next up previous
Next: Command Line Options Up: User's guide Previous: User's guide

Using External Variables

In some cases, a vector field can depend on one or several parameters and the user is interested in changing them at runtime. Moreover, for vector fields that depends on lots of constants, e.g. power or fourier expansions, it is desirable to have a separate procedure to read in those constants, rather than entering them by hand into the ODE definitions. Taylor understands external variables and external arrays. It treats them as constants when computing the taylor coefficients. Listed below is a short example.

 /*  declare some external vars */
 extern MY_FLOAT  e1, e2, coef[10], freq[10];

 diff(x,t) = e1 * y;
 diff(y,t) = -x + e2*sum( coef[i] * sin( freq[i] * t), i = 0, 9);
Let's save the above in perturbation.eq1, and ask taylor to generate a solver for us.
taylor -jet -o perturbation.c -name perturbation perturbation.eq1
taylor -name perturbation -header -o taylor.h
We'll have to write a driver for our integrator.

/* save in main3.c */
#include <stdio.h>
#include <math.h>
#include "taylor.h"
/* these are the variables the vector fields 
 * depends on.
 */
MY_FLOAT e1, e2, coef[10], freq[10];

int main(int argc, char **argv)
{
   MY_FLOAT  xx[2], t;
   double    h, abs_err, rel_err, h_return;
   double    log10abs_err, log10rel_err, endtime;
   int       i, nsteps = 1000, order=10, direction=1;
   int       step_ctrl_method=2;

   /* read in e1, e2, coef[] and freq[] 
    * here, we just assign them to some
    * values 
    */
   e1 = e2 = 1.0;
   for(i = 0; i < 10; i++) { 
       coef[i] = 1.0;
       freq[i] = 0.1*(double) i;
    }

   /* set initiaial conditions */
   xx[0] = 0.1;
   xx[1] = 0.2;
   t     = 0.0;
   /* control parameters       */
   h= 0.001;
   abs_err = 1.0e-16;
   rel_err = 1.0e-16;
   log10abs_err = log10(abs_err); 
   log10rel_err = log10(rel_err); 
   endtime = 10.0;

   /* integrate 100 steps */
   while( -- nsteps > 0 && h_return != 0.0 ) {
      /* do something with xx and t. We just print it */
      printf("%f %f %f\n", xx[0],xx[1],t);
      taylor_step_perturbation(&t, &xx[0], direction,
              step_ctrl_method,log10abs_err, log10rel_err, 
                               &endtime, &h_return, &order);
   }
}

Now we can compile perturbation.c and man3.c and run the executable.

   gcc main3.c perturbation.c -lm
   ./a.out



Maorong Zou
Tue Nov 13 09:42:48 CST 2001