VCV Rack API v2
Loading...
Searching...
No Matches
ode.hpp
Go to the documentation of this file.
1#pragma once
2#include <dsp/common.hpp>
3
4
5namespace rack {
6namespace dsp {
7
8
29template <typename T, typename F>
30void stepEuler(T t, T dt, T x[], int len, F f) {
31 T k[len];
32
33 f(t, x, k);
34 for (int i = 0; i < len; i++) {
35 x[i] += dt * k[i];
36 }
37}
38
40template <typename T, typename F>
41void stepRK2(T t, T dt, T x[], int len, F f) {
42 T k1[len];
43 T k2[len];
44 T yi[len];
45
46 f(t, x, k1);
47
48 for (int i = 0; i < len; i++) {
49 yi[i] = x[i] + k1[i] * dt / T(2);
50 }
51 f(t + dt / T(2), yi, k2);
52
53 for (int i = 0; i < len; i++) {
54 x[i] += dt * k2[i];
55 }
56}
57
59template <typename T, typename F>
60void stepRK4(T t, T dt, T x[], int len, F f) {
61 T k1[len];
62 T k2[len];
63 T k3[len];
64 T k4[len];
65 T yi[len];
66
67 f(t, x, k1);
68
69 for (int i = 0; i < len; i++) {
70 yi[i] = x[i] + k1[i] * dt / T(2);
71 }
72 f(t + dt / T(2), yi, k2);
73
74 for (int i = 0; i < len; i++) {
75 yi[i] = x[i] + k2[i] * dt / T(2);
76 }
77 f(t + dt / T(2), yi, k3);
78
79 for (int i = 0; i < len; i++) {
80 yi[i] = x[i] + k3[i] * dt;
81 }
82 f(t + dt, yi, k4);
83
84 for (int i = 0; i < len; i++) {
85 x[i] += dt * (k1[i] + T(2) * k2[i] + T(2) * k3[i] + k4[i]) / T(6);
86 }
87}
88
89
90} // namespace dsp
91} // namespace rack
void stepRK4(T t, T dt, T x[], int len, F f)
Solves an ODE system using the 4th order Runge-Kutta method.
Definition ode.hpp:60
void stepEuler(T t, T dt, T x[], int len, F f)
The callback function f in each of these stepping functions must have the signature.
Definition ode.hpp:30
void stepRK2(T t, T dt, T x[], int len, F f)
Solves an ODE system using the 2nd order Runge-Kutta method.
Definition ode.hpp:41
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9