Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::vector<double> rungekutta4order(const std::function<double(double)> &f, const MethodParams ¶ms) {
- int n = (int) ((params.x1 - params.x0) / params.h) + 1;
- std::vector<double> solution;
- double ynext;
- double yi = params.y0;
- double xi = params.x0;
- double h = params.h;
- solution.push_back(yi);
- for (int i = 1; i < n; i++) {
- double k1 = h * f(yi);
- double k2 = h * f(yi + 0.5 * k1);
- double k3 = h * f(yi + 0.5 * k2);
- double k4 = h * f(yi + k3);
- ynext = yi + (1.0 / 6.0) * (k1 + 2 * k2 + 2 * k3 + k4);
- solution.push_back(ynext);
- xi += params.x0 + i * params.h;
- yi = ynext;
- }
- return solution;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement