-
Notifications
You must be signed in to change notification settings - Fork 458
Expand file tree
/
Copy pathnyquist_test.py
More file actions
299 lines (239 loc) · 10.1 KB
/
Copy pathnyquist_test.py
File metadata and controls
299 lines (239 loc) · 10.1 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""nyquist_test.py - test Nyquist plots
RMM, 30 Jan 2021
This set of unit tests covers various Nyquist plot configurations. Because
much of the output from these tests are graphical, this file can also be run
from ipython to generate plots interactively.
"""
import pytest
import numpy as np
import matplotlib.pyplot as plt
import control as ct
pytestmark = pytest.mark.usefixtures("mplcleanup")
# Utility function for counting unstable poles of open loop (P in FBS)
def _P(sys, indent='right'):
if indent == 'right':
return (sys.pole().real > 0).sum()
elif indent == 'left':
return (sys.pole().real >= 0).sum()
elif indent == 'none':
if any(sys.pole().real == 0):
raise ValueError("indent must be left or right for imaginary pole")
else:
raise TypeError("unknown indent value")
# Utility function for counting unstable poles of closed loop (Z in FBS)
def _Z(sys):
return (sys.feedback().pole().real >= 0).sum()
# Basic tests
def test_nyquist_basic():
# Simple Nyquist plot
sys = ct.rss(5, 1, 1)
N_sys = ct.nyquist_plot(sys)
assert _Z(sys) == N_sys + _P(sys)
# Unstable system
sys = ct.tf([10], [1, 2, 2, 1])
N_sys = ct.nyquist_plot(sys)
assert _Z(sys) > 0
assert _Z(sys) == N_sys + _P(sys)
# Multiple systems - return value is final system
sys1 = ct.rss(3, 1, 1)
sys2 = ct.rss(4, 1, 1)
sys3 = ct.rss(5, 1, 1)
counts = ct.nyquist_plot([sys1, sys2, sys3])
for N_sys, sys in zip(counts, [sys1, sys2, sys3]):
assert _Z(sys) == N_sys + _P(sys)
# Nyquist plot with poles at the origin, omega specified
sys = ct.tf([1], [1, 3, 2]) * ct.tf([1], [1, 0])
omega = np.linspace(0, 1e2, 100)
count, contour = ct.nyquist_plot(sys, omega, return_contour=True)
np.testing.assert_array_equal(
contour[contour.real < 0], omega[contour.real < 0])
# Make sure things match at unmodified frequencies
np.testing.assert_almost_equal(
contour[contour.real == 0],
1j*np.linspace(0, 1e2, 100)[contour.real == 0])
# Make sure that we can turn off frequency modification
count, contour_indented = ct.nyquist_plot(
sys, np.linspace(1e-4, 1e2, 100), return_contour=True)
assert not all(contour_indented.real == 0)
count, contour = ct.nyquist_plot(
sys, np.linspace(1e-4, 1e2, 100), return_contour=True,
indent_direction='none')
np.testing.assert_almost_equal(contour, 1j*np.linspace(1e-4, 1e2, 100))
# Nyquist plot with poles at the origin, omega unspecified
sys = ct.tf([1], [1, 3, 2]) * ct.tf([1], [1, 0])
count, contour = ct.nyquist_plot(sys, return_contour=True)
assert _Z(sys) == count + _P(sys)
# Nyquist plot with poles at the origin, return contour
sys = ct.tf([1], [1, 3, 2]) * ct.tf([1], [1, 0])
count, contour = ct.nyquist_plot(sys, return_contour=True)
assert _Z(sys) == count + _P(sys)
# Nyquist plot with poles on imaginary axis, omega specified
sys = ct.tf([1], [1, 3, 2]) * ct.tf([1], [1, 0, 1])
count = ct.nyquist_plot(sys, np.linspace(1e-3, 1e1, 1000))
assert _Z(sys) == count + _P(sys)
# Nyquist plot with poles on imaginary axis, omega specified, with contour
sys = ct.tf([1], [1, 3, 2]) * ct.tf([1], [1, 0, 1])
count, contour = ct.nyquist_plot(
sys, np.linspace(1e-3, 1e1, 1000), return_contour=True)
assert _Z(sys) == count + _P(sys)
# Nyquist plot with poles on imaginary axis, return contour
sys = ct.tf([1], [1, 3, 2]) * ct.tf([1], [1, 0, 1])
count, contour = ct.nyquist_plot(sys, return_contour=True)
assert _Z(sys) == count + _P(sys)
# Nyquist plot with poles at the origin and on imaginary axis
sys = ct.tf([1], [1, 3, 2]) * ct.tf([1], [1, 0, 1]) * ct.tf([1], [1, 0])
count, contour = ct.nyquist_plot(sys, return_contour=True)
assert _Z(sys) == count + _P(sys)
# Some FBS examples, for comparison
def test_nyquist_fbs_examples():
s = ct.tf('s')
"""Run through various examples from FBS2e to compare plots"""
plt.figure()
plt.title("Figure 10.4: L(s) = 1.4 e^{-s}/(s+1)^2")
sys = ct.tf([1.4], [1, 2, 1]) * ct.tf(*ct.pade(1, 4))
count = ct.nyquist_plot(sys)
assert _Z(sys) == count + _P(sys)
plt.figure()
plt.title("Figure 10.4: L(s) = 1/(s + a)^2 with a = 0.6")
sys = 1/(s + 0.6)**3
count = ct.nyquist_plot(sys)
assert _Z(sys) == count + _P(sys)
plt.figure()
plt.title("Figure 10.6: L(s) = 1/(s (s+1)^2) - pole at the origin")
sys = 1/(s * (s+1)**2)
count = ct.nyquist_plot(sys)
assert _Z(sys) == count + _P(sys)
plt.figure()
plt.title("Figure 10.10: L(s) = 3 (s+6)^2 / (s (s+1)^2)")
sys = 3 * (s+6)**2 / (s * (s+1)**2)
count = ct.nyquist_plot(sys)
assert _Z(sys) == count + _P(sys)
plt.figure()
plt.title("Figure 10.10: L(s) = 3 (s+6)^2 / (s (s+1)^2) [zoom]")
count = ct.nyquist_plot(sys, omega_limits=[1.5, 1e3])
# Frequency limits for zoom give incorrect encirclement count
# assert _Z(sys) == count + _P(sys)
assert count == -1
@pytest.mark.parametrize("arrows", [
None, # default argument
1, 2, 3, 4, # specified number of arrows
[0.1, 0.5, 0.9], # specify arc lengths
])
def test_nyquist_arrows(arrows):
sys = ct.tf([1.4], [1, 2, 1]) * ct.tf(*ct.pade(1, 4))
plt.figure();
plt.title("L(s) = 1.4 e^{-s}/(s+1)^2 / arrows = %s" % arrows)
count = ct.nyquist_plot(sys, arrows=arrows)
assert _Z(sys) == count + _P(sys)
def test_nyquist_encirclements():
# Example 14.14: effect of friction in a cart-pendulum system
s = ct.tf('s')
sys = (0.02 * s**3 - 0.1 * s) / (s**4 + s**3 + s**2 + 0.25 * s + 0.04)
plt.figure();
count = ct.nyquist_plot(sys)
plt.title("Stable system; encirclements = %d" % count)
assert _Z(sys) == count + _P(sys)
plt.figure();
count = ct.nyquist_plot(sys * 3)
plt.title("Unstable system; encirclements = %d" % count)
assert _Z(sys * 3) == count + _P(sys * 3)
# System with pole at the origin
sys = ct.tf([3], [1, 2, 2, 1, 0])
plt.figure();
count = ct.nyquist_plot(sys)
plt.title("Pole at the origin; encirclements = %d" % count)
assert _Z(sys) == count + _P(sys)
def test_nyquist_indent():
# FBS Figure 10.10
s = ct.tf('s')
sys = 3 * (s+6)**2 / (s * (s+1)**2)
# poles: [-1, -1, 0]
plt.figure();
count = ct.nyquist_plot(sys)
plt.title("Pole at origin; indent_radius=default")
assert _Z(sys) == count + _P(sys)
# first value of default omega vector was 0.1, replaced by 0. for contour
# indent_radius is larger than 0.1 -> no extra quater circle around origin
count, contour = ct.nyquist_plot(sys, plot=False, indent_radius=.1007,
return_contour=True)
np.testing.assert_allclose(contour[0], .1007+0.j)
# second value of omega_vector is larger than indent_radius: not indented
assert np.all(contour.real[2:] == 0.)
plt.figure();
count, contour = ct.nyquist_plot(sys, indent_radius=0.01,
return_contour=True)
plt.title("Pole at origin; indent_radius=0.01; encirclements = %d" % count)
assert _Z(sys) == count + _P(sys)
# indent radius is smaller than the start of the default omega vector
# check that a quarter circle around the pole at origin has been added.
np.testing.assert_allclose(contour[:50].real**2 + contour[:50].imag**2,
0.01**2)
plt.figure();
count = ct.nyquist_plot(sys, indent_direction='left')
plt.title(
"Pole at origin; indent_direction='left'; encirclements = %d" % count)
assert _Z(sys) == count + _P(sys, indent='left')
# System with poles on the imaginary axis
sys = ct.tf([1, 1], [1, 0, 1])
# Imaginary poles with standard indentation
plt.figure();
count = ct.nyquist_plot(sys)
plt.title("Imaginary poles; encirclements = %d" % count)
assert _Z(sys) == count + _P(sys)
# Imaginary poles with indentation to the left
plt.figure();
count = ct.nyquist_plot(sys, indent_direction='left', label_freq=300)
plt.title(
"Imaginary poles; indent_direction='left'; encirclements = %d" % count)
assert _Z(sys) == count + _P(sys, indent='left')
# Imaginary poles with no indentation
plt.figure();
count = ct.nyquist_plot(
sys, np.linspace(0, 1e3, 1000), indent_direction='none')
plt.title(
"Imaginary poles; indent_direction='none'; encirclements = %d" % count)
assert _Z(sys) == count + _P(sys)
def test_nyquist_exceptions():
# MIMO not implemented
sys = ct.rss(2, 2, 2)
with pytest.raises(
ct.exception.ControlMIMONotImplemented,
match="only supports SISO"):
ct.nyquist_plot(sys)
# Legacy keywords for arrow size
sys = ct.rss(2, 1, 1)
with pytest.warns(FutureWarning, match="use `arrow_size` instead"):
ct.nyquist_plot(sys, arrow_width=8, arrow_length=6)
# Discrete time system sampled above Nyquist frequency
sys = ct.drss(2, 1, 1)
sys.dt = 0.01
with pytest.warns(UserWarning, match="above Nyquist"):
ct.nyquist_plot(sys, np.logspace(-2, 3))
if __name__ == "__main__":
#
# Interactive mode: generate plots for manual viewing
#
# Running this script in python (or better ipython) will show a collection of
# figures that should all look OK on the screeen.
#
# In interactive mode, turn on ipython interactive graphics
plt.ion()
# Start by clearing existing figures
plt.close('all')
print("Nyquist examples from FBS")
test_nyquist_fbs_examples()
print("Arrow test")
test_nyquist_arrows(None)
test_nyquist_arrows(1)
test_nyquist_arrows(3)
test_nyquist_arrows([0.1, 0.5, 0.9])
print("Stability checks")
test_nyquist_encirclements()
print("Indentation checks")
test_nyquist_indent()
print("Unusual Nyquist plot")
sys = ct.tf([1], [1, 3, 2]) * ct.tf([1], [1, 0, 1])
plt.figure()
plt.title("Poles: %s" % np.array2string(sys.pole(), precision=2, separator=','))
count = ct.nyquist_plot(sys)
assert _Z(sys) == count + _P(sys)