SHARE
TWEET

RungeKutta4order

rustamm Oct 18th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. std::vector<double> rungekutta4order(const std::function<double(double)> &f, const MethodParams &params) {
  2.     int n = (int) ((params.x1 - params.x0) / params.h) + 1;
  3.     std::vector<double> solution;
  4.     double ynext;
  5.     double yi = params.y0;
  6.     double xi = params.x0;
  7.     double h = params.h;
  8.     solution.push_back(yi);
  9.  
  10.     for (int i = 1; i < n; i++) {
  11.         double k1 = h * f(yi);
  12.         double k2 = h * f(yi + 0.5 * k1);
  13.         double k3 = h * f(yi + 0.5 * k2);
  14.         double k4 = h * f(yi + k3);
  15.  
  16.         ynext = yi + (1.0 / 6.0) * (k1 + 2 * k2 + 2 * k3 + k4);
  17.         solution.push_back(ynext);
  18.         xi += params.x0 + i * params.h;
  19.         yi = ynext;
  20.     }
  21.  
  22.     return solution;
  23. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top