-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer_functions_in_python.html
451 lines (394 loc) · 26.4 KB
/
transfer_functions_in_python.html
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<!DOCTYPE html>
<html lang="en">
<head>
<!-- 2022-07-23 Sat 05:11 -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Transfer Functions in python</title>
<meta name="generator" content="Org mode">
<meta name="author" content="ivanaf">
<link rel="stylesheet" type="text/css" href="css/org.css"/>
<link rel="icon" href="ico/favicon.ico" type="image/x- icon">
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2020 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<div class="head">
<div class="title">
<p>
<a href="index.html">Ivanaf</a>
</p>
</div>
<menu>
<ul class="org-ul">
<li><a href="index.html">Home</a></li>
<li><a href="journal.html">Journal</a></li>
<li><a href="about.html">About</a></li>
<li><a href="resume.html">Resume</a></li>
<li><a href="portfolio.html">Portfolio</a></li>
<li><a href="contact.html">Contact</a></li>
<li><a href="projects_ideas.html">Messy Ideas</a></li>
</ul>
</menu>
</div>
<p>
</p><h1>
Transfer Functions in python
</h1><p>
</p>
<p>
<span class=page-date> <small>
2018-02-27, updated 2022-07-23 — <a href='journal.html#tech' class='tech tagbutton'>tech</a>   <a href="emacs_drag-drop_pdfs_paste_html_custom_templates.html">⇦Emacs drag-drop pdfs, paste html, custom templates</a> – <a href="groebner_basis_for_linear_network_coding_in_sage.html">Groebner Basis for Linear Network Coding in Sage⇨</a>
</small> </span>
</p>
<p>
All of this is based on
<a href="http://blog.codelv.com/2013/02/control-systems-in-python-part-1.html">http://blog.codelv.com/2013/02/control-systems-in-python-part-1.html</a>
. From 2013 to 2018, the python control library has improved a lot, so
now it is relatively easier to do multiple control operations, such as
ploting to root locus. This extends the code from frmdstryr to support
discrete time using 'z', instead of only continuous time using 's'.
</p>
<p>
This also shows an example of how to use it, by solving a problem from a
6.302 lab, a class from MIT.
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-variable-name">INTERACTIVE_PLOT</span> = <span class="org-constant">True</span>
<span class="org-keyword">if</span> INTERACTIVE_PLOT:
%matplotlib notebook
<span class="org-keyword">pass</span>
<span class="org-keyword">else</span>:
%matplotlib inline
<span class="org-keyword">pass</span>
<span class="org-keyword">import</span> sympy
<span class="org-keyword">from</span> sympy <span class="org-keyword">import</span> *
sympy.init_printing()
<span class="org-variable-name">s</span> = Symbol(<span class="org-string">'s'</span>)
<span class="org-variable-name">z</span> = Symbol(<span class="org-string">'z'</span>)
<span class="org-keyword">from</span> control <span class="org-keyword">import</span> matlab
<span class="org-keyword">from</span> control <span class="org-keyword">import</span> pzmap
<span class="org-keyword">import</span> matplotlib.pyplot <span class="org-keyword">as</span> plt
<span class="org-variable-name">DEFAULT_DT</span> = 0.001
<span class="org-comment-delimiter">#</span><span class="org-comment">Converts a polynomial in z to a transfer function</span>
<span class="org-keyword">def</span> <span class="org-function-name">tfDiscrete</span>(Ts, dt = DEFAULT_DT, *args, **kwargs):
<span class="org-variable-name">tfunc</span> = Ts.simplify() <span class="org-comment-delimiter">#</span><span class="org-comment">This is necessary, otherwise you can get float errors (the result is too inexact.)</span>
<span class="org-variable-name">num</span> = Poly(tfunc.as_numer_denom()[0],z).all_coeffs()
<span class="org-variable-name">den</span> = Poly(tfunc.as_numer_denom()[1],z).all_coeffs()
<span class="org-variable-name">tf</span> = matlab.tf(<span class="org-builtin">map</span>(<span class="org-builtin">float</span>,num),<span class="org-builtin">map</span>(<span class="org-builtin">float</span>,den), dt)
<span class="org-keyword">return</span> tf
<span class="org-keyword">def</span> <span class="org-function-name">tfCont</span>(Ts, *args, **kwargs):
<span class="org-variable-name">tfunc</span> = Ts.simplify() <span class="org-comment-delimiter">#</span><span class="org-comment">This is necessary, otherwise you can get float errors (the result is too inexact.)</span>
<span class="org-variable-name">num</span> = Poly(tfunc.as_numer_denom()[0],s).all_coeffs()
<span class="org-variable-name">den</span> = Poly(tfunc.as_numer_denom()[1],s).all_coeffs()
<span class="org-variable-name">tf</span> = matlab.tf(<span class="org-builtin">map</span>(<span class="org-builtin">float</span>,num),<span class="org-builtin">map</span>(<span class="org-builtin">float</span>,den))
<span class="org-keyword">return</span> tf
<span class="org-keyword">def</span> <span class="org-function-name">tf</span>(Ts, dt = DEFAULT_DT, *args, **kwargs):
<span class="org-keyword">if</span> <span class="org-builtin">len</span>(Ts.free_symbols) > 1:
<span class="org-keyword">raise</span> <span class="org-type">ValueError</span>(<span class="org-string">'Too many free variables, '</span> + <span class="org-builtin">str</span>(Ts.free_symbols) +
<span class="org-string">' A transfer function is a polynomial in only s or z.'</span>)
<span class="org-keyword">if</span> <span class="org-builtin">len</span>(Ts.free_symbols) < 1:
<span class="org-keyword">raise</span> <span class="org-type">ValueError</span>(<span class="org-string">'Too few variables.'</span>
<span class="org-string">'A transfer function is a polynomial in s or z.'</span>)
<span class="org-keyword">if</span> z <span class="org-keyword">in</span> Ts.free_symbols :
<span class="org-keyword">return</span> tfDiscrete(Ts, dt, *args, **kwargs)
<span class="org-keyword">elif</span> s <span class="org-keyword">in</span> Ts.free_symbols:
<span class="org-keyword">return</span> tfCont(Ts, *args, **kwargs)
<span class="org-keyword">else</span>:
<span class="org-keyword">raise</span> <span class="org-type">ValueError</span>(<span class="org-string">'A transfer function is a polynomial in s or z.'</span>
<span class="org-string">'not one in '</span> + <span class="org-builtin">str</span>(Ts.free_symbols))
<span class="org-keyword">def</span> <span class="org-function-name">pole</span>(Ts, dt = DEFAULT_DT, *args,**kwargs):
<span class="org-keyword">return</span> matlab.pole(tf(Ts,dt),*args,**kwargs)
<span class="org-keyword">def</span> <span class="org-function-name">rlocus</span>(Ts, dt = DEFAULT_DT, *args,**kwargs):
plt.figure()
matlab.rlocus(tf(Ts,dt),*args,**kwargs)
plt.show()
<span class="org-keyword">def</span> <span class="org-function-name">bode</span>(Ts, dt = DEFAULT_DT, *args,**kwargs):
plt.figure()
matlab.bode(tf(Ts,dt),*args,**kwargs)
plt.show()
<span class="org-keyword">def</span> <span class="org-function-name">polezero</span>(Ts, dt = DEFAULT_DT):
plt.figure()
<span class="org-variable-name">pz</span> = pzmap.pzmap(tf(Ts,dt))
plt.show()
<span class="org-keyword">return</span> pz
<span class="org-keyword">def</span> <span class="org-function-name">damp</span>(Ts, dt = DEFAULT_DT, *args,**kwargs):
<span class="org-keyword">return</span> matlab.damp(tf(Ts,dt),*args,**kwargs)
<span class="org-keyword">def</span> <span class="org-function-name">stepResponse</span>(Ts, dt = DEFAULT_DT, *args,**kwargs):
plt.figure()
<span class="org-variable-name">tfunc</span> = tf(Ts,dt,*args,**kwargs)
<span class="org-variable-name">y</span>,<span class="org-variable-name">t</span> = matlab.step(tfunc,*args,**kwargs)
<span class="org-keyword">if</span>(<span class="org-builtin">len</span>(t)==<span class="org-builtin">len</span>(y)): <span class="org-comment-delimiter"># </span><span class="org-comment">Continuous time</span>
plt.plot(t,y)
plt.title(<span class="org-string">"Step Response"</span>)
plt.grid()
plt.xlabel(<span class="org-string">"time (s)"</span>)
plt.ylabel(<span class="org-string">"y(t)"</span>)
<span class="org-variable-name">info</span> =<span class="org-string">"Over Shoot: %f%s"</span>%(<span class="org-builtin">round</span>((y.<span class="org-builtin">max</span>()/y[-1]-1)*100,2),<span class="org-string">'%'</span>)
<span class="org-keyword">try</span>:
<span class="org-variable-name">i10</span> = <span class="org-builtin">next</span>(i <span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(0,<span class="org-builtin">len</span>(y)-1) <span class="org-keyword">if</span> y[i]>=y[-1]*.10)
<span class="org-variable-name">Tr</span> = <span class="org-builtin">round</span>(t[<span class="org-builtin">next</span>(i <span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(i10,<span class="org-builtin">len</span>(y)-1) <span class="org-keyword">if</span> y[i]>=y[-1]*.90)]-t[i10],2)
<span class="org-keyword">except</span> <span class="org-type">StopIteration</span>:
<span class="org-variable-name">Tr</span> = <span class="org-string">"unknown"</span>
<span class="org-keyword">try</span>:
<span class="org-variable-name">Ts</span> = <span class="org-builtin">round</span>(t[<span class="org-builtin">next</span>(<span class="org-builtin">len</span>(y)-i <span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(2,<span class="org-builtin">len</span>(y)-1) <span class="org-keyword">if</span> <span class="org-builtin">abs</span>(y[-i]/y[-1])>1.02 <span class="org-keyword">or</span> <span class="org-builtin">abs</span>(y[-i]/y[-1])<0.98)]-t[0],2)
<span class="org-keyword">except</span> <span class="org-type">StopIteration</span>:
<span class="org-variable-name">Ts</span> = <span class="org-string">"unknown"</span>
<span class="org-variable-name">info</span> += <span class="org-string">"\nRise Time: %s"</span>%(Tr)
<span class="org-variable-name">info</span> +=<span class="org-string">"\nSettling time: %s"</span>%(Ts)
<span class="org-keyword">print</span> info
plt.legend([info],loc=4)
plt.show()
<span class="org-keyword">else</span>: <span class="org-comment-delimiter">#</span><span class="org-comment">discrete time </span>
<span class="org-variable-name">y</span> = y[0] <span class="org-comment-delimiter">#</span><span class="org-comment">unpack value</span>
<span class="org-variable-name">t</span> = [x*dt <span class="org-keyword">for</span> x <span class="org-keyword">in</span> <span class="org-builtin">range</span>(<span class="org-builtin">len</span>(y))]
plt.plot(t, y)
plt.title(<span class="org-string">"Step Response"</span>)
plt.grid()
plt.xlabel(<span class="org-string">"time (s)"</span>)
plt.ylabel(<span class="org-string">"y[t]"</span>)
<span class="org-variable-name">info</span> =<span class="org-string">"Over Shoot: %f%s"</span>%(<span class="org-builtin">round</span>((y.<span class="org-builtin">max</span>()/y[-1]-1)*100,2),<span class="org-string">'%'</span>)
<span class="org-keyword">try</span>:
<span class="org-variable-name">i10</span> = <span class="org-builtin">next</span>(i <span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(0,<span class="org-builtin">len</span>(y)-1) <span class="org-keyword">if</span> y[i]>=y[-1]*.10)
<span class="org-variable-name">Tr</span> = <span class="org-builtin">round</span>(t[<span class="org-builtin">next</span>(i <span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(i10,<span class="org-builtin">len</span>(y)-1) <span class="org-keyword">if</span> y[i]>=y[-1]*.90)]-t[i10],2)
<span class="org-keyword">except</span> <span class="org-type">StopIteration</span>:
<span class="org-variable-name">Tr</span> = <span class="org-string">"unknown"</span>
<span class="org-keyword">try</span>:
<span class="org-variable-name">Ts</span> = <span class="org-builtin">round</span>(t[<span class="org-builtin">next</span>(<span class="org-builtin">len</span>(y)-i <span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(2,<span class="org-builtin">len</span>(y)-1) <span class="org-keyword">if</span> <span class="org-builtin">abs</span>(y[-i]/y[-1])>1.02 <span class="org-keyword">or</span> <span class="org-builtin">abs</span>(y[-i]/y[-1])<0.98)]-t[0],2)
<span class="org-keyword">except</span> <span class="org-type">StopIteration</span>:
<span class="org-variable-name">Ts</span> = <span class="org-string">"unknown"</span>
<span class="org-variable-name">info</span> += <span class="org-string">"\nRise Time: %s"</span>%(Tr)
<span class="org-variable-name">info</span> +=<span class="org-string">"\nSettling time: %s"</span>%(Ts)
plt.legend([info],loc=4)
plt.show()
<span class="org-keyword">def</span> <span class="org-function-name">impulseResponse</span>(Ts, dt = DEFAULT_DT, *args,**kwargs):
plt.figure()
<span class="org-variable-name">tfunc</span> = tf(Ts,dt,*args,**kwargs)
<span class="org-variable-name">y</span>,<span class="org-variable-name">t</span> = matlab.impulse(tfunc,*args,**kwargs)
<span class="org-keyword">if</span>(<span class="org-builtin">len</span>(t)==<span class="org-builtin">len</span>(y)): <span class="org-comment-delimiter"># </span><span class="org-comment">Continuous time</span>
plt.plot(t,y)
plt.title(<span class="org-string">"Impulse Response"</span>)
plt.grid()
plt.xlabel(<span class="org-string">"time (s)"</span>)
plt.ylabel(<span class="org-string">"y(t)"</span>)
plt.show()
<span class="org-keyword">else</span>: <span class="org-comment-delimiter">#</span><span class="org-comment">discrete time </span>
<span class="org-variable-name">y</span> = y[0] <span class="org-comment-delimiter">#</span><span class="org-comment">unpack value</span>
<span class="org-variable-name">t</span> = [x*dt <span class="org-keyword">for</span> x <span class="org-keyword">in</span> <span class="org-builtin">range</span>(<span class="org-builtin">len</span>(y))]
plt.plot(t, y)
plt.title(<span class="org-string">"Impulse Response"</span>)
plt.grid()
plt.xlabel(<span class="org-string">"time (s)"</span>)
plt.ylabel(<span class="org-string">"y[t]"</span>)
<span class="org-comment-delimiter">#</span><span class="org-comment">info ="Over Shoot: %f%s"%(round((y.max()/y[-1]-1)*100,2),'%')</span>
<span class="org-comment-delimiter">#</span><span class="org-comment">plt.legend([info],loc=4)</span>
plt.show()
<span class="org-keyword">def</span> <span class="org-function-name">oscillationPeriod</span>(Ts, dt = DEFAULT_DT, *args,**kwargs):
<span class="org-variable-name">a</span> = pole(Ts,0.001)
<span class="org-keyword">return</span> 2*3.1415/<span class="org-builtin">max</span>(a, key=<span class="org-keyword">lambda</span> x: <span class="org-builtin">abs</span>(x)).imag
</pre>
</div>
<div class="org-src-container">
<pre class="src src-python"><span class="org-variable-name">Kp</span> = Symbol(<span class="org-string">'Kp'</span>)
<span class="org-variable-name">Kd</span> = Symbol(<span class="org-string">'Kd'</span>)
<span class="org-variable-name">Ki</span> = Symbol(<span class="org-string">'Ki'</span>)
<span class="org-variable-name">p</span> = Symbol(<span class="org-string">'p'</span>)
<span class="org-variable-name">P</span> = Symbol(<span class="org-string">'P'</span>)
<span class="org-variable-name">m</span> = Symbol(<span class="org-string">'m'</span>)
<span class="org-variable-name">gamma</span> = Symbol(<span class="org-string">'y'</span>)
<span class="org-variable-name">dt</span> = Symbol(<span class="org-string">'dt'</span>)
<span class="org-variable-name">ha2w</span> = dt/(z-1)
<span class="org-variable-name">hc2a</span> = gamma
<span class="org-variable-name">hw2th</span> = dt/(z-1)
<span class="org-variable-name">hc2ap</span> = (1-p) *(z**-1)/(1-p*z**-1)*gamma
<span class="org-variable-name">H</span> = hc2a*ha2w*hw2th
<span class="org-variable-name">Hp</span> = hc2ap*ha2w*hw2th
<span class="org-variable-name">actuator</span> = H
<span class="org-variable-name">actuatorP</span> = Hp
<span class="org-variable-name">controller</span> = (Kp+Kd/(dt*m)-Kd/(dt*m)*z**(-m))
<span class="org-variable-name">sensor</span> = P
<span class="org-variable-name">G</span>=controller*actuator;
<span class="org-variable-name">Gp</span>=controller*actuatorP;
<span class="org-variable-name">sys</span> = G/(1+G*sensor)
<span class="org-variable-name">sysP</span> = Gp/(1+Gp*sensor)
<span class="org-variable-name">sys</span> = sys.simplify()
<span class="org-variable-name">sysP</span> = sysP.simplify()
pprint(sys)
<span class="org-keyword">print</span>(<span class="org-string">"\n\n ------------------------- \n\n"</span>)
pprint(sysP)
</pre>
</div>
<pre class="example">
⎛ m m⎞
dt⋅y⋅⎝Kd⋅z - Kd + Kp⋅dt⋅m⋅z ⎠
────────────────────────────────────────────────
⎛ m m⎞ m 2
P⋅dt⋅y⋅⎝Kd⋅z - Kd + Kp⋅dt⋅m⋅z ⎠ + m⋅z ⋅(z - 1)
-------------------------
⎛ m m⎞
dt⋅y⋅(p - 1)⋅⎝Kd⋅z - Kd + Kp⋅dt⋅m⋅z ⎠
────────────────────────────────────────────────────────────────
⎛ m m⎞ m 2
P⋅dt⋅y⋅(p - 1)⋅⎝Kd⋅z - Kd + Kp⋅dt⋅m⋅z ⎠ + m⋅z ⋅(p - z)⋅(z - 1)
</pre>
<div class="org-src-container">
<pre class="src src-python"><span class="org-comment-delimiter">#</span><span class="org-comment">Calculating the value of p</span>
<span class="org-comment-delimiter"># </span><span class="org-comment">N number of steps untill we are 50% done. 1 - p**n = 1/2. p = 1/2.*(1./n)</span>
<span class="org-variable-name">n</span> = 65.
<span class="org-variable-name">pval</span> = 1/2.**(1/n)
<span class="org-keyword">print</span> pval
<span class="org-comment-delimiter">#</span><span class="org-comment">We want the period to be 530 when Kp=10 and Kd=1</span>
<span class="org-comment-delimiter">#</span><span class="org-comment">By trial and error:</span>
<span class="org-variable-name">subs</span> = {gamma:13.4,
Kp:10,
Kd:1,
Ki:0,
m:3,
P:1,
p:pval,
dt:0.001}
<span class="org-keyword">print</span>(oscillationPeriod(sysP.subs(subs),0.001))
</pre>
</div>
<pre class="example">
0.989392853996
530.6524349460201
</pre>
<div class="org-src-container">
<pre class="src src-python"><span class="org-comment-delimiter"># </span><span class="org-comment">Now we want to find the best Kp and Kd</span>
<span class="org-comment-delimiter"># </span><span class="org-comment">Brute Force approach</span>
<span class="org-variable-name">a</span>=[]
<span class="org-keyword">for</span> kp <span class="org-keyword">in</span> <span class="org-builtin">range</span>(1,40):
<span class="org-keyword">for</span> kd <span class="org-keyword">in</span> <span class="org-builtin">range</span>(1,40):
<span class="org-variable-name">kpp</span> = kp/2.
<span class="org-variable-name">kdd</span> = kd/10.
<span class="org-variable-name">subs</span>[Kp] = kpp
<span class="org-variable-name">subs</span>[Kd] = kdd
a.append((kpp,kdd,<span class="org-builtin">max</span>(<span class="org-builtin">abs</span>(pole(sysP.subs(subs),0.001)))))
<span class="org-variable-name">kpp</span>, <span class="org-variable-name">kdd</span> , <span class="org-variable-name">magnitude</span> = <span class="org-builtin">min</span>(a, key=<span class="org-keyword">lambda</span> x: x[2])
<span class="org-keyword">print</span> (kpp, kdd , magnitude)
<span class="org-comment-delimiter"># </span><span class="org-comment">Non Bruteforce</span>
<span class="org-keyword">from</span> scipy.optimize <span class="org-keyword">import</span> minimize
<span class="org-keyword">def</span> <span class="org-function-name">func_min</span>(x):
<span class="org-variable-name">kpp</span> = x[0]
<span class="org-variable-name">kdd</span> = x[1]
<span class="org-variable-name">subs</span>[Kp] = kpp
<span class="org-variable-name">subs</span>[Kd] = kdd
<span class="org-keyword">return</span> <span class="org-builtin">max</span>(<span class="org-builtin">abs</span>(pole(sysP.subs(subs),0.001)))
<span class="org-variable-name">x0</span> = [8, 1]
<span class="org-variable-name">res</span> = minimize(func_min, x0, method=<span class="org-string">'nelder-mead'</span>, options={<span class="org-string">'disp'</span>: <span class="org-constant">True</span>})
<span class="org-keyword">print</span> res
</pre>
</div>
<pre class="example">
(1.5, 0.6, 0.9965544944796353)
Optimization terminated successfully.
Current function value: 0.996489
Iterations: 125
Function evaluations: 237
final_simplex: (array([[0.30518369, 0.26037023],
[0.30511368, 0.26035043],
[0.30511147, 0.26034981]]), array([0.99648941, 0.99648941, 0.99648943]))
fun: 0.99648940643066
message: 'Optimization terminated successfully.'
nfev: 237
nit: 125
status: 0
success: True
x: array([0.30518369, 0.26037023])
</pre>
<div class="org-src-container">
<pre class="src src-python"><span class="org-comment-delimiter"># </span><span class="org-comment">Comparing both results</span>
<span class="org-keyword">print</span> subs
<span class="org-variable-name">subs</span>[Kp] = 1.5
<span class="org-variable-name">subs</span>[Kd] = 0.6
stepResponse(sysP.subs(subs), 0.001, T=[x*0.001 <span class="org-keyword">for</span> x <span class="org-keyword">in</span> <span class="org-builtin">range</span>(3000)])
impulseResponse(sysP.subs(subs), 0.001, T=[x*0.001 <span class="org-keyword">for</span> x <span class="org-keyword">in</span> <span class="org-builtin">range</span>(3000)])
bode(sysP.subs(subs),0.001)
<span class="org-keyword">print</span> polezero(sysP.subs(subs),0.001)
<span class="org-variable-name">subs</span>[Kp] = 0.30533799
<span class="org-variable-name">subs</span>[Kd] = 0.26041388
stepResponse(sysP.subs(subs), 0.001, T=[x*0.001 <span class="org-keyword">for</span> x <span class="org-keyword">in</span> <span class="org-builtin">range</span>(3000)])
impulseResponse(sysP.subs(subs), 0.001, T=[x*0.001 <span class="org-keyword">for</span> x <span class="org-keyword">in</span> <span class="org-builtin">range</span>(3000)])
bode(sysP.subs(subs),0.001)
<span class="org-keyword">print</span> polezero(sysP.subs(subs),0.001)
</pre>
</div>
<pre class="example">
{Ki: 0, Kd: 0.26041388, m: 3, p: 0.9893928539959366, dt: 0.001, P: 1, Kp: 0.30533799, y: 13.4}
</pre>
<pre class="example">
(array([ 0.99648939+0.00019111j, 0.99648939-0.00019111j,
0.99648941+0. j, -0.02267371+0. j,
0.01129919+0.02054887j, 0.01129919-0.02054887j]), array([-0.49941512+0.86501235j, -0.49941512-0.86501235j,
0.99883023+0. j]))
</pre>
</div></div>
<br>
<div class="comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'ivanaf'; // Required - Replace '<example>' with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
var showComments = function() {
var button = document.getElementById('comment-button')
button.style.display = 'none'
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
};
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<button id="comment-button" onclick="showComments()">Show comments</button>
</div>
<div><div>
</div>
<div id="postamble" class="status">
<p class="author">Author: Ivan Tadeu Ferreira Antunes Filho</p>
<p class="date">Date: 2022-07-23 Sat 05:11</p>
<p class="author">Github: <a href="https://github.com/itf/">github.com/itf</a></p>
<p class="creator">Made with <a href="https://www.gnu.org/software/emacs/">Emacs</a> 27.1 (<a href="https://orgmode.org">Org</a> mode 9.3) and <a href="https://github.com/itf/org-export-head">Org export head</a> </p>
</div>
</body>
</html>