-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunge_kutta.cpp
43 lines (36 loc) · 1.31 KB
/
runge_kutta.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "test_utils.hpp"
#include "numeric/runge_kutta.hpp"
double sine(double x) { return sin(x); }
double cosine(double x) { return cos(x); }
void unit_test_simpson() {
for (int n = 1; n <= 20; n++) {
println("simpson(N={}): {:2.15f}", 1 << n, simpson(1 << n, 0.0, 1.0, sine));
}
}
void unit_test_runge_kutta_explicit() {
auto f = [](double, double, double z) { return z; };
auto g = [](double, double y, double z) { return (1 - y * y) * z - y; };
int N = 10000, S = 50;
auto trace1 = explicit_rk4_two(N, 0.0, 1.0, 0.0, 25.0, f, g);
println("--- Explicit van der pol");
for (int i = 0; i < S; i++) {
auto [x, y, z] = trace1[N / S * i];
println("t:{:7.3f} y:{:7.3f} z:{:7.3f}", x, y, z);
}
}
void unit_test_runge_kutta_implicit() {
auto f = [](double x, double y, double z) { return 2 * cos(z) - sin(y) - x; };
int N = 10000, S = 50;
auto trace1 = implicit_rk1_one(N, 20, 0.0, 1.0, 1.0, 0.5, 1e-9, f);
println("--- Implicit trig");
for (int i = 0; i < S; i++) {
auto [x, y, z] = trace1[N / S * i];
println("t:{:7.3f} y:{:7.3f} z:{:7.3f} f: {}", x, y, z, f(x, y, z));
}
}
int main() {
RUN_BLOCK(unit_test_simpson());
RUN_BLOCK(unit_test_runge_kutta_explicit());
RUN_BLOCK(unit_test_runge_kutta_implicit());
return 0;
}