-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_pendulum.cpp
100 lines (89 loc) · 2.72 KB
/
code_pendulum.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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <getopt.h>
using namespace std;
double range = 6;
double h = .01;
double omega = 4;
double lambda = 0;
double F = 0;
double sigma = 5;
double f(double t, double z1, double z2){
return F*cos(sigma*t)-lambda*z2-pow(omega, 2)*sin(z1);
}
int main(int argc, char **argv){
fstream file;
double z1 = 1, z2 = 1, t = 0, k1, k2;
int option_index;
bool fase = false, keep = false;
while(( option_index = getopt(argc, argv, "l:f:s:o:r:d:x:v:keh" )) != -1){
switch (option_index){
case 'h':
cout << "--------------------------\n";
cout << "-l to set lambda, the damping constant \n";
cout << "-f to set the amplitude of the external force, F*cos(sigma*t)\n";
cout << "-o to set de natural frequency (omega)\n";
cout << "-s to set the external frenquency\n";
cout << "-r to set the graph range (begin in zero)\n";
cout << "-x to set the initial position\n";
cout << "-v to set the initial velocity\n";
cout << "-d to set the step, (deltaT)\n";
cout << "-k to show the phase space\n";
cout << "-e to keep the data on the data file\n";
cout << "--------------------------\n";
break;
case 'l':
lambda = stod(optarg);
break;
case 'f':
F = stod(optarg);
break;
case 'o':
omega = stod(optarg);
break;
case 's':
sigma = stod(optarg);
break;
case 'r':
range = stod(optarg);
break;
case 'x':
z1 = stod(optarg);
break;
case 'v':
z2 = stod(optarg);
break;
case 'd':
h = stod(optarg);
break;
case 'k':
fase = true;
break;
case 'e':
keep = true;
break;
default :
return 1;
}
}
file.open("2order.txt");
for (double i = 0; i <= range; i += h){
if(!fase){
file << t << " " << z1 << endl;
}else{
file << z1 << " " << z2 << endl;
}
z1 += z2 * h;
k1 = h * f(t, z1, z2);
k2 = h * f(t + h, z1, z2 + k1);
z2 += (k1 + k2)/2;
t += h;
}
file.close();
system("gnuplot --persist -e \"plot '2order.txt'\" ");
if(!keep)
system("> 2order.txt");
return 0;
}