Transfer Functions in python
February 27, 2018 Updated: July 23, 2022 #techAll of this is based on http://blog.codelv.com/2013/02/control-systems-in-python-part-1.html . 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'.
This also shows an example of how to use it, by solving a problem from a 6.302 lab, a class from MIT.
= True
%
pass
%
pass
=
=
= 0.001
#Converts a polynomial in z to a transfer function
= #This is necessary, otherwise you can get float errors (the result is too inexact.)
=
=
=
return
= #This is necessary, otherwise you can get float errors (the result is too inexact.)
=
=
=
return
return
return
return
=
return
return
=
, =
# Continuous time
=%
=
=
=
=
=
+= %
+=%
print
#discrete time
= #unpack value
=
=%
=
=
=
=
=
+= %
+=%
=
, =
# Continuous time
#discrete time
= #unpack value
=
#info ="Over Shoot: %f%s"%(round((y.max()/y[-1]-1)*100,2),'%')
#plt.legend([info],loc=4)
=
return 2*3.1415/.
=
=
=
=
=
=
=
=
= /
=
= /
= */*
= **
= **
=
=
=
=
=*;
=*;
= /
= /
=
=
⎛ ⎞
⋅ ⋅⎝ ⋅ - + ⋅ ⋅ ⋅ ⎠
────────────────────────────────────────────────
⎛ ⎞ 2
⋅ ⋅ ⋅⎝ ⋅ - + ⋅ ⋅ ⋅ ⎠ + ⋅ ⋅
-------------------
⎛ ⎞
⋅ ⋅ ⋅⎝ ⋅ - + ⋅ ⋅ ⋅ ⎠
────────────────────────────────────────────────────────────────
⎛ ⎞ 2
⋅ ⋅ ⋅ ⋅⎝ ⋅ - + ⋅ ⋅ ⋅ ⎠ + ⋅ ⋅ ⋅
#Calculating the value of p
# N number of steps untill we are 50% done. 1 - p**n = 1/2. p = 1/2.*(1./n)
= 65.
= 1/2.**
print
#We want the period to be 530 when Kp=10 and Kd=1
#By trial and error:
=
0.989392853996
530.6524349460201
# Now we want to find the best Kp and Kd
# Brute Force approach
=
= /2.
= /10.
=
=
, , =
# Non Bruteforce
=
=
=
=
return
=
=
print
.
: 0.996489
: 125
: 237
:
: 0.99648940643066
:
: 237
: 125
: 0
: True
:
# Comparing both results
print
= 1.5
= 0.6
print
= 0.30533799
= 0.26041388
print
(array([ 0.99652992+0.00699788j, 0.99652992-0.00699788j,
0.99650654+0. j, -0.0297408 +0. j,
0.01478363+0.02733623j, 0.01478363-0.02733623j]), array([-0.49875621+0.8638711j, -0.49875621-0.8638711j,
0.99751243+0. j]))
(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]))