-
Notifications
You must be signed in to change notification settings - Fork 0
/
ODE.cpp
121 lines (104 loc) · 2.27 KB
/
ODE.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "ODE.hpp"
/*
// solve a vector of differential equations
//vector<float> EulerMethod(vector<float> y0, float t0, float t_end,
float dt, dydt_func dydt)
{
vector<float> y_end;
// verify that t0 < t_end
if (t0 >= t_end)
{
throw "Bad arguments for t0 and t_end. Make sure that t0 < t_end.";
}
if (dt <=0)
{
throw "Bad argument for dt: Make sure that dt > 0.";
}
// solve ODE using Euler's method for all elements in y0
// y_0i is the ith element of vector y0
for (float y_0i : y0)
{
// initialise variables
float t = t0;
float y = y_0i;
while (t < t_end)
{
// step
y += dydt(t, y)*dt;
t += dt;
}
y_end.push_back(y);
}
return y_end;
}
*/
/*
// solve scalar differential equation
float EulerMethod(float y0, float t0, float t_end,
float dt, dydt_func dydt)
{
// verify that t0 < t_end
if (t0 >= t_end)
{
throw "Bad arguments for t0 and t_end. Make sure that t0 < t_end.";
}
if (dt <=0)
{
throw "Bad argument for dt: Make sure that dt > 0.";
}
// solve ODE using Euler's method
float t = t0;
float y = y0;
while (t < t_end)
{
// step
y += dydt(t, y)*dt;
t += dt;
}
return y;
}
*/
// solve 3x3 matrix differential equation
mat3 EulerMethod(mat3 M0, float t0, float t_end, float dt,
mat3 dMdt)
{
// verify that t0 < t_end
if (t0 >= t_end)
{
throw "Bad arguments for t0 and t_end. Make sure that t0 < t_end.";
}
if (dt <=0)
{
throw "Bad argument for dt: Make sure that dt > 0.";
}
float t = t0;
mat3 M = M0;
while (t < t_end)
{
M += dMdt*dt;
t += dt;
}
return M;
}
// solve vec3 differential equation
vec3 EulerMethod(vec3 V0, float t0, float t_end, float dt,
vec3 dV3dt)
{
// verify that t0 < t_end
if (t0 >= t_end)
{
throw "Bad arguments for t0 and t_end. Make sure that t0 < t_end.";
}
if (dt <=0)
{
throw "Bad argument for dt: Make sure that dt > 0.";
}
float t = t0;
vec3 V = V0;
while (t < t_end)
{
V += dV3dt*dt;
t += dt;
}
return V;
}