-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplots.py
More file actions
389 lines (285 loc) · 9.81 KB
/
Copy pathplots.py
File metadata and controls
389 lines (285 loc) · 9.81 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import numpy as np
from .figures import PZmap_Figure, Step_Figure,Impulse_Figure,Bode_Figure, Nichols_Figure, Rlocus_Figure
def generic_layout(fig,x_lim=None,y_lim=None,x_title=None,y_title=None):
if x_title is not None:
fig.set_x_title(x_title)
if y_title is not None:
fig.set_y_title(y_title)
if x_lim is not None:
fig.set_x_lim(x_lim)
if y_lim is not None:
fig.set_y_lim(y_lim)
return fig
def pzmap(sys_list,x_lim=None,y_lim=None,x_title=None,y_title=None):
"""
Returns a pole-zero plot of the continuous or discrete-time systems `sys_list`.
Parameters
----------
sys_list : system or list of systems
A single system or a list of systems to analyse
x_lim : list (optional)
A list of two element that defines the min and max value for the x axis
y_lim : list (optional)
A list of two element that defines the min and max value for the y axis
x_title : str (optional)
The x axis name
y_title : str (optional)
The y axis name
Returns
-------
fig : plotly figure
A plotly figure
Example
-------
.. code ::
import control as ctl
from control_plotly import pzmap
sys1 = ctl.tf([1],[2,1,1])
sys2 = ctl.tf([1],[1,0.5,1])
pzmap([sys1,sys2])
.. image:: img/pzmap.png
:alt: alternate text
:align: center
"""
fig = PZmap_Figure()
if type(sys_list) is not list:
sys_list = [sys_list]
for index,sys in enumerate(sys_list):
label = "sys{}".format(index+1)
fig.add_plot(sys,label=label)
fig = generic_layout(fig,x_lim=x_lim,y_lim=y_lim,x_title=x_title,y_title=y_title)
return fig.show()
def generic_time_fig(fig,sys_list,t=None):
if type(sys_list) is not list:
sys_list = [sys_list]
for index,sys in enumerate(sys_list):
label = "sys{}".format(index+1)
fig.add_plot(sys,T=t,label=label)
return fig
def step(sys_list,t=None,x_lim=None,y_lim=None,x_title=None,y_title=None):
"""
Returns the step response plot of the continuous or discrete-time systems `sys_list`.
Parameters
----------
sys_list : system or list of systems
A single system or a list of systems to analyse
t : numpy vector (optional)
The base time vector
x_lim : list (optional)
A list of two element that defines the min and max value for the x axis
y_lim : list (optional)
A list of two element that defines the min and max value for the y axis
x_title : str (optional)
The x axis name
y_title : str (optional)
The y axis name
Returns
-------
fig : plotly figure
A plotly figure
Example
-------
.. code ::
import control as ctl
from control_plotly import step
sys1 = ctl.tf([1],[2,1,1])
sys2 = ctl.tf([1],[1,0.5,1])
t = np.arange(0,20,0.01)
step([sys1,sys2],t=t)
.. image:: img/step.png
:alt: alternate text
:align: center
"""
fig = Step_Figure()
fig = generic_time_fig(fig,sys_list,t=t)
fig = generic_layout(fig,x_lim=x_lim,y_lim=y_lim,x_title=x_title,y_title=y_title)
return fig.show()
def impulse(sys_list,t=None,x_lim=None,y_lim=None,x_title=None,y_title=None):
"""
Returns the impulse response of the continuous or discrete-time systems `sys_list`.
Parameters
----------
sys_list : system or list of systems
A single system or a list of systems to analyse
t : numpy vector (optional)
The base time vector
x_lim : list (optional)
A list of two element that defines the min and max value for the x axis
y_lim : list (optional)
A list of two element that defines the min and max value for the y axis
x_title : str (optional)
The x axis name
y_title : str (optional)
The y axis name
Returns
-------
fig : plotly figure
A plotly figure
Example
-------
.. code ::
import control as ctl
from control_plotly import impulse
sys1 = ctl.tf([1],[2,1,1])
sys2 = ctl.tf([1],[1,0.5,1])
t = np.arange(0,20,0.01)
impulse([sys1,sys2],t=t)
.. image:: img/impulse.png
:alt: alternate text
:align: center
"""
fig = Impulse_Figure()
fig = generic_time_fig(fig,sys_list,t=t)
fig = generic_layout(fig,x_lim=x_lim,y_lim=y_lim,x_title=x_title,y_title=y_title)
return fig.show()
def generic_frequency_fig(fig,sys_list,w=None):
if type(sys_list) is not list:
sys_list = [sys_list]
for index,sys in enumerate(sys_list):
label = "sys{}".format(index+1)
fig.add_plot(sys,w=w,label=label)
return fig
def bode(sys_list,w=None,x_lim=None,y_lim=None,dB=True,Hz=False,deg=True,log_x=True):
"""
Returns the impulse response of the continuous or discrete-time systems `sys_list`.
Parameters
----------
sys_list : system or list of systems
A single system or a list of systems to analyse
w : numpy vector (optional)
The base angular frequency vector (in rad/s)
x_lim : list (optional)
A list of two element that defines the min and max value for the x axis
y_lim : list (optional)
A list of two element that defines the min and max value for the y axis
dB : boolean (optional)
Use a logarithmic scale for the magnitude plot
Hz : boolean (optional)
Use frequency in Hz for the x axis
deg: boolean (optional)
Use angle in degree for the phase plot.
Returns
-------
fig : plotly figure
A plotly figure
Example
-------
.. code ::
import control as ctl
from control_plotly import bode
sys1 = ctl.tf([1],[2,1,1])
sys2 = ctl.tf([1],[1,0.5,1])
w = np.logspace(-1,1,100)
bode([sys1,sys2],w=w)
.. image:: img/bode.png
:alt: alternate text
:align: center
"""
fig = Bode_Figure(dB=dB,Hz=Hz,deg=deg,log_x=log_x)
fig = generic_frequency_fig(fig,sys_list,w=w)
fig = generic_layout(fig,x_lim=x_lim,y_lim=y_lim)
return fig.show()
def nichols(sys_list,w=None,x_lim=None,y_lim=None,cm=np.array([6,3,1,.5,.25,0,-1,-3,-6,-12,-20,-40]),cp=np.array([1,5,10,20,30,50,90,120,150,180]),show_grid=True,show_mag=True,show_phase=True):
"""
Returns the nichols chart of the continuous or discrete-time systems `sys_list`.
Parameters
----------
sys_list : system or list of systems
A single system or a list of systems to analyse
w : numpy vector (optional)
The base angular frequency vector (in rad/s)
x_lim : list (optional)
A list of two element that defines the min and max value for the x axis
y_lim : list (optional)
A list of two element that defines the min and max value for the y axis
cm : numpy vector (optional)
A numpy vector containing the list of contour gain (in dB)
cp : numpy vector (optional)
A numpy vector containing the list of contour phase (in deg)
show_grid: boolean (optional)
Add the nichols grid
show_mag: boolean (optional)
Show the nichols magnitude grid
show_phase: boolean (optional)
Show the nichols phase grid
Returns
-------
fig : plotly figure
A plotly figure
Example
-------
.. code ::
import control as ctl
from control_plotly import nichols
sys1 = ctl.tf([1],[2,1,1])
sys2 = ctl.tf([1],[1,0.5,1])
nichols([sys1,sys2])
.. image:: img/nichols.png
:alt: alternate text
:align: center
.. code ::
import control as ctl
import numpy as np
from control_plotly import nichols
sys = ctl.tf([1],[2,1,1])
nichols(sys,show_phase=False,cm=np.array([0.5,-6]),x_lim=[-200,0],y_lim=[-40,10])
.. image:: img/nichols2.png
:alt: alternate text
:align: center
"""
if type(cm) is list:
cm = np.array(cm)
if type(cp) is list:
cp = np.array(cp)
fig = Nichols_Figure(show_mag=show_mag,show_phase=show_phase)
fig = generic_frequency_fig(fig,sys_list,w=w)
if show_grid:
fig.add_grid(cm=cm,cp=cp)
fig = generic_layout(fig,x_lim=x_lim,y_lim=y_lim)
return fig.show()
def rlocus(sys,k=None,x_lim=None,y_lim=None,show_grid=False,wn=np.arange(0.1,1.1,0.1),m=np.arange(0,1,0.1),):
"""
Returns the root locus chart of the continuous or discrete-time systems `sys_list`.
Parameters
----------
sys : system
A single system
k : numpy vector (optional)
The vector of feedback gains
x_lim : list (optional)
A list of two element that defines the min and max value for the x axis
y_lim : list (optional)
A list of two element that defines the min and max value for the y axis
show_grid: boolean (optional)
Add the discrete to continuous pole grid
Returns
-------
fig : plotly figure
A plotly figure
Example
-------
.. code ::
import control as ctl
from control_plotly import rlocus
sys = ctl.tf([2,5,1],[1,2,3])
rlocus(sys)
.. image:: img/rlocus.png
:alt: alternate text
:align: center
"""
if k is None:
k = np.logspace(-2,1.2,200)
if type(k) is list:
k = np.array(k)
if type(wn) is list:
wn = np.array(wn)
if type(m) is list:
m = np.array(m)
if type(sys) is list:
raise ValueError("The rlocus function can only plot one system (not a list of systems)")
fig = Rlocus_Figure()
fig.add_plot(sys,k)
if show_grid:
fig.add_grid(angle_list=wn,m_list=m)
fig = generic_layout(fig,x_lim=x_lim,y_lim=y_lim)
return fig.show()