-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab-6-complex-runge-kutta.py
136 lines (109 loc) · 4.79 KB
/
lab-6-complex-runge-kutta.py
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
import matplotlib.pylab
R1 = 50.0
R2 = 60.0
R3 = 30.0
L2 = 5.45
i_min = 1
i_max = 2
cubic_spline_h = i_max - i_min
C2 = 0.00154
L_max = 47
L_min = 4.7
current_time = 0.0
time_step = 0.00001
def u1(time_curr):
time_const = 0.007
time_real = time_curr - float(time_curr // (2 * time_const)) * 2 * time_const
if time_real > time_const:
return 0
else:
return float(time_real / time_const) * 10
def l1(i1):
if i1 <= i_min:
return L_max
elif i1 >= i_max:
return L_min
else:
return \
(cubic_spline_coefficients[0](i1) * L_max +
cubic_spline_coefficients[1](i1) * L_min) / (cubic_spline_h ** 3) + \
(cubic_spline_coefficients[2](i1) * 0 +
cubic_spline_coefficients[3](i1) * 0) / cubic_spline_h ** 2
cubic_spline_coefficients = [
lambda x: (2 * (x - i_min) + cubic_spline_h) * ((i_max - x) ** 2),
lambda x: (2 * (i_max - x) + cubic_spline_h) * ((x - i_min) ** 2),
lambda x: (x - i_min) * (i_max - x) ** 2,
lambda x: (x - i_max) * (i_min - x) ** 2
]
differential_equations = [
lambda t, u20, i10, i20: 1 / C2 * i20,
lambda t, u20, i10, i20: 1 / l1(i10) * (u1(t) - i10 * (R1 + R3) + i20 * R3),
lambda t, u20, i10, i20: 1 / L2 * (i10 * R3 - u20 - i20 * (R2 + R3))
]
# x0 - U2
# x1 - i1
# x2 - i2
def get_runge_kutta_coefficients(ARG_PREVIOUS):
global current_time
global time_step
NEW_COEFFICIENTS = [[0 for _ in range(len(ARG_PREVIOUS))] for _ in range(4)]
# K1
for i in range(len(ARG_PREVIOUS)):
NEW_COEFFICIENTS[0][i] = time_step * differential_equations[i](current_time,
ARG_PREVIOUS[0],
ARG_PREVIOUS[1],
ARG_PREVIOUS[2])
# K2
for i in range(len(ARG_PREVIOUS)):
NEW_COEFFICIENTS[1][i] = time_step * differential_equations[i](current_time + time_step / 2,
ARG_PREVIOUS[0] + NEW_COEFFICIENTS[0][0] / 2,
ARG_PREVIOUS[1] + NEW_COEFFICIENTS[0][1] / 2,
ARG_PREVIOUS[2] + NEW_COEFFICIENTS[0][2] / 2)
# K3
for i in range(len(ARG_PREVIOUS)):
NEW_COEFFICIENTS[2][i] = time_step * differential_equations[i](current_time + time_step / 2,
ARG_PREVIOUS[0] + NEW_COEFFICIENTS[1][0] / 2,
ARG_PREVIOUS[1] + NEW_COEFFICIENTS[1][1] / 2,
ARG_PREVIOUS[2] + NEW_COEFFICIENTS[1][2] / 2)
# K4
for i in range(len(ARG_PREVIOUS)):
NEW_COEFFICIENTS[3][i] = time_step * differential_equations[i](current_time + time_step,
ARG_PREVIOUS[0] + NEW_COEFFICIENTS[2][0],
ARG_PREVIOUS[1] + NEW_COEFFICIENTS[2][1],
ARG_PREVIOUS[2] + NEW_COEFFICIENTS[2][2])
return NEW_COEFFICIENTS
def runge_kutta_formula(ARG_PREVIOUS):
global current_time
NEW_COEFFICIENTS = get_runge_kutta_coefficients(ARG_PREVIOUS)
NEW_ARGS = []
for j in range(len(ARG_PREVIOUS)):
K = []
for i in range(len(NEW_COEFFICIENTS)):
K.append(NEW_COEFFICIENTS[i][j])
NEW_ARGS.append(ARG_PREVIOUS[j] + calculate_through_coefficients(K))
current_time += time_step
return NEW_ARGS
def calculate_through_coefficients(K):
return 1 / 6 * (K[0] + 2 * (K[1] + K[2]) + K[3])
def build_graph(x_ax, y_ax, y_ax_name, x_ax_name='T'):
matplotlib.pylab.plot(x_ax, y_ax)
matplotlib.pylab.ylabel(y_ax_name)
matplotlib.pylab.xlabel(x_ax_name)
matplotlib.pylab.savefig("graphs/lab6_" + y_ax_name + ".png")
matplotlib.pylab.clf()
if __name__ == '__main__':
time = [i * time_step for i in range(500000)]
current_values = [0, 0, 0]
res_u2 = []
res_i1 = []
res_i2 = []
for t in time:
res_u2.append(current_values[0])
res_i1.append(current_values[1])
res_i2.append(current_values[2])
current_values = runge_kutta_formula(current_values)
build_graph([i * time_step for i in range(10000)], [u1(t) for t in [i * time_step for i in range(10000)]], "U1")
build_graph(time, res_u2, "U2")
build_graph(time, res_i1, "i1")
build_graph(time, res_i2, "i2")
build_graph([i * 0.001 for i in range(0, 3000)], [l1(i * 0.001) for i in range(0, 3000)], "L1", "i")