#include #include #include #include #include #include "nr.h" using namespace std; #define PI 3.141592653589793 Vec_DP *xp_p; Mat_DP *yp_p; const int n_ord = 3; const double a = 8.0; // global parameter const double ya = 2.0; // initial condition double yex(double t) { // analytical solution of the ODE double a2; a2 = 1.0/(1.0+a*a); return( (ya - a2)*exp(-t) + a2*( cos(a*t) + a*sin(a*t) ) ); } void deriv(const DP t, Vec_I_DP &y, Vec_O_DP &f) // RHS of ODE { f[0] = cos(a*t) - y[0]; } void adamsbash(int k, Vec_IO_DP &y, const DP t, const DP h, Mat_IO_DP yp, Vec_IO_DP xp, const int n_ord, Vec_O_DP &yout ) { const int NMAXORD = 5; static DP beta[NMAXORD][NMAXORD] = {{1.0, 0.0, 0.0, 0.0, 0.0}, {(3.0/2.0), (-1.0/2.0), 0.0, 0.0, 0.0}, {(23.0/12.0),(-16.0/12.0),(5.0/12.0), 0.0, 0.0}, {(55.0/24.0),(-59.0/24.0),(37.0/24.0),(-9.0/24.0), 0.0}, {(1901.0/720.0),(-2774.0/720.0),(2616.0/720.0),(-1274.0/720.0), (251.0/720.0)}}; int i,j; int nvar=y.size(); Vec_DP sum(nvar); if (n_ord > NMAXORD || n_ord < 1) NR::nrerror("Formula not present"); for (i=0; i end of starting procedure ..." << endl; // multistep method ( Adams-Bashforth, order ) for (j=n_ord; j analytical solution: " << endl; cout << "# j | t | yex(t) | f(t,yex(t) " << endl; cout << "#======================================================= " << endl; tt = 0.0; for (int j=0;j numerical solution; n_ord = " << n_ord << endl; cout << "# j | t | yp(t) | f(t,yp(t) " << endl; cout << "# ======================================================= " << endl; for (int j=0;j error: n_ord = " << n_ord << endl; cout << "# j | t | yp(t) | yex(t) | yp(t) - yex(t)"; cout << endl; cout << "# ============= =========================================" << endl; for (int j=0;j