#include #include #include #include "rk.h" /* Runge-Kutta method solve an ordinary differential equation dq/dt = f(t,q) where q is an n-dimensional vector consisting of n variables. */ /* solve an ordinary differential equation dq/dt = f(t,q) from the k-th time step to the (k+1)-th time step input: *pt time at the k-th time step dt time step (interval) n dimension of vector q q[] value of vector q at the k-th time step diff() function to compute f(t,q) function diff() should be in the following form: diff(double t, int n, double q[], double dotq[]) t time n dimension of vector q q[] vector (n-dimensional array) dotq[] array where value of f(t,q) is stored (n-dimensional array) output: *pt time at the (k+1)-th time step q[] value of vector q at the (k+1)-th time step */ extern void RKOneStep (double *pt, double dt, int n, double q[], void diff(double, int, double *, double *)) { int i; double q1[MaxVars], q2[MaxVars], q3[MaxVars]; double d1[MaxVars], d2[MaxVars], d3[MaxVars], d4[MaxVars]; diff(*pt, n, q, d1); /* comp. d1 */ for(i=0;i MaxVars) { fprintf(stderr, "%d exceeds MaxVars (%d)\n", n, MaxVars); fprintf(stderr, "increase MaxVars in opt.h and recompile all\n"); exit(1); } for(k=1; k<=cnt; k++) { #if DEBUG printf("<<<<< count = %d >>>>>\n", k); #endif RKOneStep(pt, dt, n, q, diff); RKfprint(fd, *pt, n, q); } }