DiffEq - Modern C++ ODE Integration Library 1.0.0
High-performance C++ library for solving ODEs with async signal processing
Loading...
Searching...
No Matches
rk4_integrator_usage.cpp
1#include <iostream>
2#include <vector>
3#include <array>
4#include <iomanip>
5#include <memory>
6#include <diffeq.hpp>
7
8// Example 1: Simple exponential decay
9// dy/dt = -k*y, where k is the decay constant
10void exponential_decay(double t, const std::vector<double>& y, std::vector<double>& dydt) {
11 const double k = 0.5; // decay constant
12 dydt[0] = -k * y[0];
13}
14
15// Example 2: Lorenz attractor (simplified)
16// dx/dt = sigma*(y - x)
17// dy/dt = x*(rho - z) - y
18// dz/dt = xy - beta*z
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;
23
24 double x = state[0];
25 double y = state[1];
26 double z = state[2];
27
28 dydt[0] = sigma * (y - x);
29 dydt[1] = x * (rho - z) - y;
30 dydt[2] = x * y - beta * z;
31}
32
33// Example 3: Damped harmonic oscillator
34// d^2x/dt^2 + 2*gamma*(dx/dt) + omega^2*x = 0
35// State: [x, dx/dt]
36void damped_oscillator(float t, const std::array<float, 2>& state, std::array<float, 2>& dydt) {
37 const float gamma = 0.1f; // damping coefficient
38 const float omega_sq = 4.0f; // omega^2 = 4
39
40 dydt[0] = state[1]; // dx/dt = v
41 dydt[1] = -2.0f * gamma * state[1] - omega_sq * state[0]; // dv/dt = -2*gamma*v - omega^2*x
42}
43
44int main() {
45 std::cout << "RK4 Integrator Usage Examples" << std::endl;
46 std::cout << "==============================" << std::endl;
47 std::cout << std::fixed << std::setprecision(6);
48
49 // Example 1: Exponential Decay
50 std::cout << "\n1. Exponential Decay (dy/dt = -0.5*y)" << std::endl;
51 std::cout << "-------------------------------------" << std::endl;
52
53 diffeq::RK4Integrator<std::vector<double>> decay_integrator(exponential_decay);
54 std::vector<double> decay_state = {2.0}; // y(0) = 2
55
56 std::cout << "Time\tValue" << std::endl;
57 std::cout << "0.0\t" << decay_state[0] << std::endl;
58
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;
62 }
63
64 // Example 2: Lorenz System
65 std::cout << "\n2. Lorenz Attractor (first 20 steps)" << std::endl;
66 std::cout << "------------------------------------" << std::endl;
67
68 diffeq::RK4Integrator<std::vector<double>> lorenz_integrator(lorenz_system);
69 std::vector<double> lorenz_state = {1.0, 1.0, 1.0}; // Initial conditions
70
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;
73
74 for (int i = 0; i < 20; ++i) {
75 lorenz_integrator.step(lorenz_state, 0.01);
76 if (i % 5 == 4) { // Print every 5th step
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;
81 }
82 }
83
84 // Example 3: Damped Harmonic Oscillator with float precision
85 std::cout << "\n3. Damped Harmonic Oscillator (float precision)" << std::endl;
86 std::cout << "----------------------------------------------" << std::endl;
87
88 diffeq::RK4Integrator<std::array<float, 2>> oscillator_integrator(damped_oscillator);
89 std::array<float, 2> oscillator_state = {1.0f, 0.0f}; // x(0) = 1, v(0) = 0
90
91 std::cout << "Time\tPosition\tVelocity" << std::endl;
92 std::cout << "0.0\t" << oscillator_state[0] << "\t\t" << oscillator_state[1] << std::endl;
93
94 for (int i = 0; i < 50; ++i) {
95 oscillator_integrator.step(oscillator_state, 0.1f);
96 if (i % 10 == 9) { // Print every 10th step
97 std::cout << oscillator_integrator.current_time() << "\t"
98 << oscillator_state[0] << "\t\t"
99 << oscillator_state[1] << std::endl;
100 }
101 }
102
103 // Example 4: Using polymorphism
104 std::cout << "\n4. Polymorphic Usage" << std::endl;
105 std::cout << "-------------------" << std::endl;
106
107 auto integrator = std::make_unique<diffeq::RK4Integrator<std::vector<double>>>(exponential_decay);
108 AbstractIntegrator<std::vector<double>>* base_ptr = integrator.get();
109
110 std::vector<double> poly_state = {5.0};
111 std::cout << "Initial: t=" << base_ptr->current_time() << ", y=" << poly_state[0] << std::endl;
112
113 base_ptr->integrate(poly_state, 0.1, 2.0); // Integrate from t=0 to t=2 with dt=0.1
114 std::cout << "Final: t=" << base_ptr->current_time() << ", y=" << poly_state[0] << std::endl;
115
116 return 0;
117}
Classical 4th-order Runge-Kutta integrator.
Definition rk4.hpp:19
Modern C++ ODE Integration Library with Real-time Signal Processing.