-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunge-kutta_method.cc
162 lines (147 loc) · 4.67 KB
/
runge-kutta_method.cc
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "runge-kutta_method.hh"
#include <cmath>
boundary_conditions get_ode_system_right_part_value (
boundary_conditions const & bc_value,
double const parameter)
{
boundary_conditions answer;
answer.x1 = bc_value.x2;
answer.x2 = bc_value.p2 + bc_value.x1 * cos (parameter * bc_value.x2);
answer.p1 = -bc_value.p2 * cos (parameter * bc_value.x2);
answer.p2 = -bc_value.p1 + parameter * bc_value.p2 * bc_value.x1 * sin (parameter * bc_value.x2);
return answer;
}
boundary_conditions runge_kutta_method_4 (
double const step_length,
size_t const total_steps,
boundary_conditions const & bc_initial,
double const parameter
)
{
boundary_conditions q1, q2, q3, q4, answer;
answer = bc_initial;
for (size_t i = 0; i < total_steps; ++i) {
q1 = get_ode_system_right_part_value (answer, parameter) * step_length;
q2 = get_ode_system_right_part_value (answer + q1 * .5, parameter) * step_length;
q3 = get_ode_system_right_part_value (answer + q2 * .5, parameter) * step_length;
q4 = get_ode_system_right_part_value (answer + q3, parameter) * step_length;
answer += (q1 + q2 * 2 + q3 * 2 + q4) / 6;
}
return answer;
}
/*
// it works, but say no to spaghetti
boundary_conditions runge_kutta_method_6 (
double const step_length,
size_t const total_steps,
boundary_conditions const & bc_initial,
double const parameter
)
{
boundary_conditions q1, q2, q3, q4, q5, q6, answer;
answer = bc_initial;
for (size_t i = 0; i < total_steps; ++i) {
q1 = get_ode_system_right_part_value (answer, parameter)
* step_length;
q2 = get_ode_system_right_part_value (answer +
q1 * 1./4,
parameter)
* step_length;
q3 = get_ode_system_right_part_value (answer +
q1 * 3./32 +
q2 * 9./32,
parameter)
* step_length;
q4 = get_ode_system_right_part_value (answer +
q1 * 1932./2197 +
q2 * -7200./2197 +
q3 * 7296./2197,
parameter)
* step_length;
q5 = get_ode_system_right_part_value (answer +
q1 * 439./216 +
q2 * -8 +
q3 * 3680./513 +
q4 * -845./4104,
parameter)
* step_length;
q6 = get_ode_system_right_part_value (answer +
q1 * -8./27 +
q2 * 2 +
q3 * -3544./2565 +
q4 * 1859./4104 +
q5 * -11./40,
parameter)
* step_length;
answer += (
q1 * 16./135 +
q2 * 0. +
q3 * 6656./12825 +
q4 * 28561./56430 +
q5 * -9./50 +
q6 * 2./55
);
}
return answer;
}
*/
boundary_conditions runge_kutta_method_6 (
double const step_length,
size_t const total_steps,
boundary_conditions const & bc_initial,
double const parameter
)
{
size_t const TOTAL_INNER_STEPS = 6;
boundary_conditions inner_steps[TOTAL_INNER_STEPS];
double const sum_coef[TOTAL_INNER_STEPS] = {
16./135, 0., 6656./12825, 28561./56430, -9./50, 2./55
};
double const inner_step_coef[TOTAL_INNER_STEPS - 1][TOTAL_INNER_STEPS - 1] = {
{1./4, 0., 0., 0., 0. },
{3./32, 9./32, 0., 0., 0. },
{1932./2197, -7200./2197, 7296./2197, 0., 0. },
{439./216, -8., 3680./513, -845./4104, 0. },
{-8./27, 2., -3544./2565, 1859./4104, -11./40}
};
boundary_conditions inner_step_helper;
boundary_conditions answer = bc_initial;
for (
size_t tot_steps_cnt = 0;
tot_steps_cnt < total_steps;
++tot_steps_cnt
)
{
for (
size_t in_steps_cnt = 0;
in_steps_cnt < TOTAL_INNER_STEPS;
++in_steps_cnt
)
{
inner_step_helper = answer;
for (size_t i = 0; i < in_steps_cnt; ++i) {
inner_step_helper += inner_steps[i] * inner_step_coef[in_steps_cnt - 1][i];
}
inner_steps[in_steps_cnt] = get_ode_system_right_part_value (inner_step_helper, parameter) * step_length;
}
for (
size_t in_steps_cnt = 0;
in_steps_cnt < TOTAL_INNER_STEPS;
++in_steps_cnt
)
{
answer += inner_steps[in_steps_cnt] * sum_coef[in_steps_cnt];
}
}
return answer;
}
boundary_conditions integrator (
double const step_length,
size_t const total_steps,
boundary_conditions const & bc_initial,
double const parameter
)
{
return runge_kutta_method_6 (step_length, total_steps, bc_initial, parameter);
// return runge_kutta_method_4 (step_length, total_steps, bc_initial, parameter);
}