-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_pointer.cpp
136 lines (110 loc) · 3.02 KB
/
main_pointer.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <vector>
#include <cmath>
#include "rk4.h"
/*
* IMPORTANT
*
* If you want to use additional parameters in a function containing
* the right parts of the ode system, you need to create a structure
* of type "params" and initialize it BEFORE defining the ode function.
*
*/
struct params {
float k = 0.003;
float m = 110;
float km = k/m;
} params;
/*
* IMPORTANT
*
* Any function on the right side of the odu must be defined using
* the following prototype:
*
* float* %FUNC_NAME% (float t, float* y, struct params *p)
*
* in which:
* :float t: - default argument, define as time, [s].
* :float*: y - integration variables.
* :struct params *p: - structure with additional parameters for the
* function of the right parts of the odu system
*
*/
float* func3(
float t,
float* y,
struct params *p
) {
float G = 9.81;
float k = p->k;
float m = p->m;
return new float[4] {
y[1],
static_cast<float>(-k / m * pow(y[1], 3)),
y[3],
static_cast<float>(-k / m * pow(y[3], 3) - G)
};
}
float* func2(
float t,
float *y,
struct params *p
) {
float G = 9.81;
return new float[4]{
y[1],
static_cast<float>(-p->km * y[1] * sqrt(pow(y[1], 2) + pow(y[3], 2))),
y[3],
static_cast<float>(-p->km * y[3] * sqrt(pow(y[1], 2) + pow(y[3], 2)) - G)
};
}
int main() {
// Initialization of initial values
// Number of equations of the given ODE system
int NUM = 4;
// Starting speed
float v0 = 950;
// Y-axis angle
float beta = 30;
// Initial conditional
float *y0;
y0 = new float[4]{
0, // x0
static_cast<float>(v0 * cos(beta * M_PI / 180)), // Vx
0, // y0
static_cast<float>(v0 * sin(beta * M_PI / 180)) // Vy
};
// Initialize necessary variables
std::vector<float*> ans_iter = {y0};
float* y;
float FlightTime, Distance, Height, y4_old = 0;
// Solving a given system of ODE using (std::vector<float>) rk_iteration()
// with some conditional
for (float t = 0; t < UINT16_MAX; t += 0.01) {
y = rk4_iteration(NUM, &func2, ¶ms, t, ans_iter.back(), 0.01);
// Max height
if (y[3] * y4_old < 0) Height = y[2];
// Flight distance and flight time
if (y[3] < 0 && y[2] <= 0) {
FlightTime = t;
Distance = y[0];
break;
}
y4_old = y[3];
ans_iter.push_back(y);
}
// Output of the obtained values
for (int i = 0; i < ans_iter.size(); i++) {
for (int j = 0; j < NUM; j++) {
std::cout << ans_iter[i][j] << " ";
}
std::cout << "\n";
}
// Some statistics about the shot
std::cout << "\n\n";
std::cout << "Flight Time = " << FlightTime << "\n";
std::cout << "Distance = " << Distance << "\n";
std::cout << "Max Height = " << Height << "\n";
delete y0;
return 0;
}