//======================================================================= // Chapter 9, code 1: adaptive RKF45 for one first-order ODE x' = f(t,x). // Translated from the revised Fortran companion code. // Based on Ode1_Adaptive.m by Alexander Godunov (March 2022). // // The driver supplies initial data and opens the data file. The RKF45 // function contains the stages and ALL adaptive step decisions. // It prints and saves the initial point and every accepted point. // // Example: x' = 100/(1+10000*t*t), x(-1) = atan(-100). // Exact: x(t) = atan(100*t). The rapid change near t = 0 makes the // reduction and subsequent growth of the step size visible. // To solve another equation, change the initial data and function f. // Set check_exact = false unless you also change the exact formula. //======================================================================= #include #include #include #include #include #include // Declarations keep the driver at the top. double f(double t, double x); bool rkf45(double (*f)(double,double), double t0, double x0, double t_end, double h_first, double tolerance, std::ofstream& data, double& xf, int& accepted, int& rejected); int main() { //--------------- Initial data: edit here ----------------------------- double t0 = -1.0; double x0 = std::atan(100.0*t0); double t_end = 1.0; double h_first = 0.1; double tolerance = 1.0e-6; // Absolute local error tolerance bool check_exact = true; //------------------------------------------------------------------- std::ofstream data("ch09_rkf45_scalar.dat"); if (!data) { std::cerr << "Cannot open ch09_rkf45_scalar.dat.\n"; return 1; } std::cout << "Adaptive Runge-Kutta-Fehlberg RKF45\n" << "Tolerance: " << std::scientific << std::setprecision(4) << tolerance << '\n' << std::setw(10) << "t" << ' ' << std::setw(18) << "numerical x" << ' ' << std::setw(14) << "h" << '\n'; data << "# t x h\n" << std::scientific << std::setprecision(16); double xf; int accepted, rejected; if (!rkf45(f,t0,x0,t_end,h_first,tolerance,data,xf,accepted,rejected)) return 1; data.close(); std::cout << "Accepted steps: " << std::setw(8) << accepted << '\n' << "Rejected trials:" << std::setw(8) << rejected << '\n' << "Final x: " << std::fixed << std::setprecision(12) << std::setw(18) << xf << '\n'; if (check_exact) { double exact = std::atan(100.0*t_end); // DEFAULT example only std::cout << "Exact final x: " << std::setw(18) << exact << '\n' << "Abs error: " << std::scientific << std::setprecision(4) << std::setw(12) << std::abs(xf-exact) << '\n'; } std::cout << "Data file: ch09_rkf45_scalar.dat\n"; return 0; } double f(double t, double x) { // Supply x' = f(t,x) here. This example depends only on t. // x is available for equations that depend on both t and x. (void)x; return 100.0/(1.0+10000.0*t*t); } bool rkf45(double (*f)(double,double), double t0, double x0, double t_end, double h_first, double tolerance, std::ofstream& data, double& xf, int& accepted, int& rejected) { // Integrate forward with adaptive Fehlberg RKF45. // Six stages give fourth- and fifth-order estimates. Advance with // the FIFTH-order value; their difference estimates local error. // The tolerance does not bound total accumulated error. // xf and the counters are returned through reference arguments (&). // Return false if the integration cannot be completed. const int max_attempts = 100000; const double h_min = 1.0e-12; if (t_end <= t0 || h_first <= 0.0 || tolerance <= 0.0) { std::cerr << "Use t_end > t0, h_first > 0, and tolerance > 0.\n"; return false; } double t = t0; double x = x0; double h = h_first; accepted = 0; rejected = 0; int attempts = 0; std::cout << std::fixed << std::setprecision(6) << std::setw(10) << t << ' ' << std::setprecision(12) << std::setw(18) << x << ' ' << std::scientific << std::setprecision(6) << std::setw(14) << 0.0 << '\n'; data << t << ' ' << x << ' ' << 0.0 << '\n'; while (t < t_end) { // Ignore only a machine-rounding gap at the endpoint. double roundoff = 64.0*std::numeric_limits::epsilon() * std::max(1.0,std::abs(t_end)); if (t_end-t <= roundoff) break; if (attempts >= max_attempts) { std::cerr << "Maximum number of trials exceeded.\n"; return false; } h = std::min(h,t_end-t); double tf = t+h; if (h < h_min || tf <= t) { std::cerr << "Step too small to advance time.\n"; return false; } ++attempts; // Fehlberg stages: each k includes the factor h. double k1 = h*f(t,x); double k2 = h*f(t+h/4.0,x+k1/4.0); double k3 = h*f(t+3.0*h/8.0,x+3.0*k1/32.0+9.0*k2/32.0); double k4 = h*f(t+12.0*h/13.0,x+1932.0*k1/2197.0 -7200.0*k2/2197.0+7296.0*k3/2197.0); double k5 = h*f(t+h,x+439.0*k1/216.0-8.0*k2 +3680.0*k3/513.0-845.0*k4/4104.0); double k6 = h*f(t+h/2.0,x-8.0*k1/27.0+2.0*k2 -3544.0*k3/2565.0+1859.0*k4/4104.0-11.0*k5/40.0); // Fifth-order candidate and absolute embedded difference. double trial = x+16.0*k1/135.0+6656.0*k3/12825.0 +28561.0*k4/56430.0-9.0*k5/50.0+2.0*k6/55.0; double error = std::abs(k1/360.0-128.0*k3/4275.0 -2197.0*k4/75240.0+k5/50.0+2.0*k6/55.0); if (error <= tolerance) { if (h == t_end-t) tf = t_end; t = tf; x = trial; ++accepted; // h is the step used to reach this accepted point. std::cout << std::fixed << std::setprecision(6) << std::setw(10) << t << ' ' << std::setprecision(12) << std::setw(18) << x << ' ' << std::scientific << std::setprecision(6) << std::setw(14) << h << '\n'; data << t << ' ' << x << ' ' << h << '\n'; } else { ++rejected; // Keep both t and x unchanged. } // Embedded error scales approximately as h^5. // Safety factor 0.9; limit growth and shrinkage. double factor; if (error == 0.0) factor = 2.0; else factor = 0.9*std::pow(tolerance/error,0.2); if (error <= tolerance) h *= std::min(2.0,factor); else h *= std::max(0.2,factor); } xf = x; return true; }