10void exponential_decay(
double t,
const std::vector<double>& y, std::vector<double>& dydt) {
19void lorenz_system(
double t,
const std::vector<double>& state, std::vector<double>& dydt) {
20 const double sigma = 10.0;
21 const double rho = 28.0;
22 const double beta = 8.0/3.0;
28 dydt[0] = sigma * (y - x);
29 dydt[1] = x * (rho - z) - y;
30 dydt[2] = x * y - beta * z;
36void damped_oscillator(
float t,
const std::array<float, 2>& state, std::array<float, 2>& dydt) {
37 const float gamma = 0.1f;
38 const float omega_sq = 4.0f;
41 dydt[1] = -2.0f * gamma * state[1] - omega_sq * state[0];
45 std::cout <<
"RK4 Integrator Usage Examples" << std::endl;
46 std::cout <<
"==============================" << std::endl;
47 std::cout << std::fixed << std::setprecision(6);
50 std::cout <<
"\n1. Exponential Decay (dy/dt = -0.5*y)" << std::endl;
51 std::cout <<
"-------------------------------------" << std::endl;
54 std::vector<double> decay_state = {2.0};
56 std::cout <<
"Time\tValue" << std::endl;
57 std::cout <<
"0.0\t" << decay_state[0] << std::endl;
59 for (
int i = 0; i < 10; ++i) {
60 decay_integrator.step(decay_state, 0.5);
61 std::cout << decay_integrator.current_time() <<
"\t" << decay_state[0] << std::endl;
65 std::cout <<
"\n2. Lorenz Attractor (first 20 steps)" << std::endl;
66 std::cout <<
"------------------------------------" << std::endl;
69 std::vector<double> lorenz_state = {1.0, 1.0, 1.0};
71 std::cout <<
"Time\tX\t\tY\t\tZ" << std::endl;
72 std::cout <<
"0.0\t" << lorenz_state[0] <<
"\t\t" << lorenz_state[1] <<
"\t\t" << lorenz_state[2] << std::endl;
74 for (
int i = 0; i < 20; ++i) {
75 lorenz_integrator.step(lorenz_state, 0.01);
77 std::cout << lorenz_integrator.current_time() <<
"\t"
78 << lorenz_state[0] <<
"\t\t"
79 << lorenz_state[1] <<
"\t\t"
80 << lorenz_state[2] << std::endl;
85 std::cout <<
"\n3. Damped Harmonic Oscillator (float precision)" << std::endl;
86 std::cout <<
"----------------------------------------------" << std::endl;
89 std::array<float, 2> oscillator_state = {1.0f, 0.0f};
91 std::cout <<
"Time\tPosition\tVelocity" << std::endl;
92 std::cout <<
"0.0\t" << oscillator_state[0] <<
"\t\t" << oscillator_state[1] << std::endl;
94 for (
int i = 0; i < 50; ++i) {
95 oscillator_integrator.step(oscillator_state, 0.1f);
97 std::cout << oscillator_integrator.current_time() <<
"\t"
98 << oscillator_state[0] <<
"\t\t"
99 << oscillator_state[1] << std::endl;
104 std::cout <<
"\n4. Polymorphic Usage" << std::endl;
105 std::cout <<
"-------------------" << std::endl;
107 auto integrator = std::make_unique<diffeq::RK4Integrator<std::vector<double>>>(exponential_decay);
108 AbstractIntegrator<std::vector<double>>* base_ptr = integrator.get();
110 std::vector<double> poly_state = {5.0};
111 std::cout <<
"Initial: t=" << base_ptr->current_time() <<
", y=" << poly_state[0] << std::endl;
113 base_ptr->integrate(poly_state, 0.1, 2.0);
114 std::cout <<
"Final: t=" << base_ptr->current_time() <<
", y=" << poly_state[0] << std::endl;
Classical 4th-order Runge-Kutta integrator.
Modern C++ ODE Integration Library with Real-time Signal Processing.