#include "NumMeth.h" void rk4(double x[], int nX, double t, double tau, void (*derivsRK)(double x[], double t, double param[], double deriv[]), double param[]) { // Runge-Kutta integrator (4th order) // Inputs // x Current value of dependent variable // nX Number of elements in dependent variable x // t Independent variable (usually time) // tau Step size (usually time step) // derivsRK Right hand side of the ODE; derivsRK is the // name of the function which returns dx/dt // Calling format derivsRK(x,t,param,dxdt). // param Extra parameters passed to derivsRK // Output // x New value of x after a step of size tau double *F1, *F2, *F3, *F4, *xtemp; F1 = new double [nX+1]; F2 = new double [nX+1]; F3 = new double [nX+1]; F4 = new double [nX+1]; xtemp = new double [nX+1]; //* Evaluate F1 = f(x,t). (*derivsRK)( x, t, param, F1 ); //* Evaluate F2 = f( x+tau*F1/2, t+tau/2 ). double half_tau = 0.5*tau; double t_half = t + half_tau; int i; for( i=1; i<=nX; i++ ) xtemp[i] = x[i] + half_tau*F1[i]; (*derivsRK)( xtemp, t_half, param, F2 ); //* Evaluate F3 = f( x+tau*F2/2, t+tau/2 ). for( i=1; i<=nX; i++ ) xtemp[i] = x[i] + half_tau*F2[i]; (*derivsRK)( xtemp, t_half, param, F3 ); //* Evaluate F4 = f( x+tau*F3, t+tau ). double t_full = t + tau; for( i=1; i<=nX; i++ ) xtemp[i] = x[i] + tau*F3[i]; (*derivsRK)( xtemp, t_full, param, F4 ); //* Return x(t+tau) computed from fourth-order R-K. for( i=1; i<=nX; i++ ) x[i] += tau/6.*(F1[i] + F4[i] + 2.*(F2[i]+F3[i])); delete [] F1, F2, F3, F4, xtemp; }