it solves \frac{\mathrm{d} y}{\mathrm{d} x}= \frac{\left ( x-y \right )}{2} given x for given initial conditions There can be many such equations
#include <cassert>
#include <iostream>
#include <vector>
static double change(
double x,
double y) {
return ((x - y) / 2.0); }
double rungeKutta(
double init_x,
const double &init_y,
const double &x,
auto n =
static_cast<uint64_t
>((x - init_x) /
h);
double y = init_y;
for (int i = 1; i <= n; ++i) {
k[1] =
h *
change(init_x + 0.5 *
h, y + 0.5 * k[0]);
k[2] =
h *
change(init_x + 0.5 *
h, y + 0.5 * k[1]);
k[3] =
h *
change(init_x +
h, y + k[2]);
y += (1.0 / 6.0) * (k[0] + 2 * k[1] + 2 * k[2] + k[3]);
}
return y;
}
}
}
std::cout <<
"The Runge Kutta function will be tested on the basis of "
"precomputed values\n";
<< "\n";
2, 3, 4, 0.2);
assert(valfirst == 3.10363932323749570);
<< "\n";
1, 2, 5, 0.1);
assert(valsec == 3.40600589380261409);
<< "\n";
-1, 3, 4, 0.1);
assert(valthird == 2.49251005860244268);
}
return 0;
}
void test()
Definition: caesar_cipher.cpp:100
int main()
Main function.
Definition: graph_coloring.cpp:112
int h(int key)
Definition: hash_search.cpp:45
Functions for Runge Kutta fourth order method.
static double change(double x, double y)
for using the vector container
Definition: rungekutta.cpp:33
double rungeKutta(double init_x, const double &init_y, const double &x, const double &h)
the Runge Kutta method finds the value of integration of a function in the given limits....
Definition: rungekutta.cpp:57