-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_2.py
More file actions
92 lines (70 loc) · 2.78 KB
/
lab_2.py
File metadata and controls
92 lines (70 loc) · 2.78 KB
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
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import TransferFunction, freqresp
def plot_bode(system, frequencies):
"""Строит амплитудно-фазовую характеристику системы."""
w, H = freqresp(system, frequencies)
plt.figure(figsize=(10, 6))
plt.subplot(2, 1, 1)
plt.plot(w, 20 * np.log10(np.abs(H)))
plt.title('АФЧХ')
plt.xlabel('Частота (рад/с)')
plt.ylabel('Амплитуда (дБ)')
plt.grid()
plt.subplot(2, 1, 2)
plt.plot(w, np.angle(H))
plt.title('Фазочастотная характеристика')
plt.xlabel('Частота (рад/с)')
plt.ylabel('Фаза (рад)')
plt.grid()
def plot_nyquist(H):
"""Строит годограф Найквиста."""
plt.figure(figsize=(6, 6))
plt.plot(np.real(H), np.imag(H))
plt.title('Годограф Найквиста')
plt.xlabel('Re')
plt.ylabel('Im')
plt.grid()
def plot_mikhailov(denominator, frequencies):
"""Строит годограф Михайлова."""
omega = 1j * frequencies
poly_values = np.polyval(denominator, omega)
plt.figure(figsize=(6, 6))
plt.plot(np.real(poly_values), np.imag(poly_values), label='Годограф Михайлова')
plt.axis([-10.0, 100.0, -80.0, 15.0]) # Подогнать под ваш случай
plt.title('Годограф Михайлова')
plt.xlabel('Re')
plt.ylabel('Im')
plt.grid()
plt.legend()
def plot_root_locus(numerator, denominator, K_range):
"""Строит корневой годограф (локус корней)."""
roots_list = []
for K in K_range:
closed_loop_den = np.polyadd(denominator, np.polymul(numerator, [K]))
roots = np.roots(closed_loop_den)
roots_list.append(roots)
roots_array = np.array(roots_list)
plt.figure(figsize=(8, 6))
for i in range(roots_array.shape[1]):
plt.plot(np.real(roots_array[:, i]), np.imag(roots_array[:, i]), label=f'Корень {i+1}')
plt.title('Корневой годограф')
plt.xlabel('Re')
plt.ylabel('Im')
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)
plt.grid()
plt.legend()
def main():
numerator = [1]
denominator = [1, 3, 4, 3, 1]
system = TransferFunction(numerator, denominator)
frequencies = np.logspace(-2, 2, 1000)
_, H = freqresp(system, frequencies)
plot_bode(system, frequencies)
plot_nyquist(H)
plot_mikhailov(denominator, frequencies)
plot_root_locus(numerator, denominator, np.linspace(0, 100, 1000))
plt.show()
if __name__ == "__main__":
main()