-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspring.cpp
64 lines (51 loc) · 1.37 KB
/
spring.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
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
// declare variables
double m, k, x, v, t_max, dt, t, a;
vector<double> t_list, x_list, v_list;
// mass, spring constant, initial position and velocity
m = 1;
k = 1;
x = 0;
v = 1;
// simulation time and timestep
t_max = 100;
dt = 0.1;
// Euler integration
for (t = 0; t <= t_max; t = t + dt) {
// append current state to trajectories
t_list.push_back(t);
x_list.push_back(x);
v_list.push_back(v);
// calculate new position and velocity
a = -k * x / m;
x = x + dt * v;
v = v + dt * a;
}
// Write the trajectories to file
ofstream fout;
fout.open("trajectories.txt");
if (fout) { // file opened successfully
for (int i = 0; i < t_list.size(); i = i + 1) {
fout << t_list[i] << ' ' << x_list[i] << ' ' << v_list[i] << endl;
}
} else { // file did not open successfully
cout << "Could not open trajectory file for writing" << endl;
}
/* The file can be loaded and visualised in Python as follows:
import numpy as np
import matplotlib.pyplot as plt
results = np.loadtxt('trajectories.txt')
plt.figure(1)
plt.clf()
plt.xlabel('time (s)')
plt.grid()
plt.plot(results[:, 0], results[:, 1], label='x (m)')
plt.plot(results[:, 0], results[:, 2], label='v (m/s)')
plt.legend()
plt.show()
*/
}