# Graphics - PTN Encirclement-checkpoint

``````

In [3]:

import numpy as np
import control as cn
import Algorithms as alg
import matplotlib.pyplot as plt
%matplotlib
from Algorithms import TUBScolorscale,cm2in

MSC = lambda MS, color: plt.Circle((-1 ,0),1/MS, color =color, alpha=0.4)
MTC = lambda MT, color: plt.Circle((-MT**2/(MT**2 - 1),0),MT/(MT**2 -1), color =color, alpha = 0.4)
MCC = lambda M : plt.Circle((-(2*M**2-2*M+1)/(2*M*(M-1)),0), (2*M-1)/(2*M*(M-1)), color = 'b', fill = None,linestyle = 'dashed', alpha = 0.4)
MC = lambda MS,MT,color: plt.Circle((-(MS*(2*MT-1)-MT+1)/(2*MS*(MT-1)),0),(MS+MT-1)/(2*MS*(MT-1)), color =color, alpha = 0.4)

``````
``````

Using matplotlib backend: Qt5Agg

``````
``````

In [9]:

# Matplotlib plot
plt.rcParams['svg.fonttype'] = 'none'
plt.style.use('seaborn-whitegrid')

# Make a system with TSum = 100 and T = TSum/order
TSum = 100.
plt.clf()
fig, ax = plt.subplots(3,3,figsize=(10,10))
fig2, ax2 = plt.subplots(1,figsize=(10,10))
ax2.cla()
row = 0
col = 0
for order in range(1,10):
# Get the current time constant
T = TSum/order
print(T)
# Make a transfer function
G = cn.tf(1,[T,1])
G = 5*G**order
# Step experiment
y,t = cn.step(G)
u = np.ones_like(t)
# FOTD identification
k,t,l = alg.Integral_Identification(y,u,t)
# Make a controller
c, b = alg.AMIGO_Tune(k,t,l)
KY = cn.tf(c[0],1)+cn.tf(c[1],[1,0])
KR = cn.tf(c[0],1)+cn.tf(c[1],[1,0])
# Open Loop
OL = G*KR
# Compute the sensitivity
S = 1/(1+G*KY)
# Complementary
T = G*KR/(1+G*KY)
# Compute the Open Loop Nyquist Plot
real,imag, w = cn.nyquist(OL, np.logspace(-5,5,10000))
ax[row,col].cla()
ax[row,col].plot(real,imag)
# Compute the maximum Sensitivity
mag, phase, freq = cn.freqresp(S, np.logspace(-5,5,10000))
MS = np.max(mag)
# Compute the maximum Complementary Sensitivity
mag, phase, freq = cn.freqresp(T,  np.logspace(-5,5,10000))
MT = np.max(mag)
print(order,MS,MT)
# Compute the circles
ax[row,col].plot(-1,0,'r+')
ax[row,col].set_xlim(-3,3)
ax[row,col].set_ylim(-3,3)
ax[row,col].set_title('Order '+str(order))
# Second plot
ax2.plot(-1,0,'r+')
ax2.plot(real,imag,color=TUBScolorscale[order])
ax2.set_xlim(-3,3)
ax2.set_ylim(-3,3)
if order%3 != 0:
row = row
col = col+1
else:
row = row+1
col = 0
plt.show()

``````
``````

10.0
(1, 0.99999999999987876, 0.9999999952484715)
5.0
(2, 1.1831182291247999, 0.99999999607340095)
3.33333333333
(3, 1.2390999905130877, 0.99999999332077605)
2.5
(4, 1.3276894201485749, 0.99999999617449231)
2.0
(5, 1.4901804877001028, 0.99999999978944931)
1.66666666667
(6, 1.8162137221596941, 1.1747089647633286)
1.42857142857
(7, 2.6567128355175318, 1.9455865730105666)
1.25
(8, 8.597201301871193, 7.8411264231910343)
1.11111111111
(9, 4.2280569556118461, 5.0151373866240485)

``````
``````

In [15]:

t,y = cn.step_response(T)
plt.clf()
plt.plot(t,y)
plt.show()

``````
``````

``````
``````

In [54]:

# Get the circles
MS = np.max(abs(re_S+1j*im_S))
MT = np.max(abs(re_T+1j*im_T))
M = 1.4
plt.clf
msc = plt.Circle((-1,0),1/MS, color = 'r', alpha = 0.4)
mtc = plt.Circle((-MT**2/(MT**2-1),0), MT/(MT**2-1), color = 'b', alpha = 0.4)
mc = plt.Circle((-(2*M**2-2*M+1)/(2*M*(M-1)),0),(2*M-1)/(2*M*(M-1)),color = 'g', alpha = 0.4)
mcc = plt.Circle((-(MS*(2*MT-1)-MT+1)/(2*MS*(MT-1)) ,0),(MS+MT-1)/(2*MS*(MT-1)), color = None, alpha=0.4)
fig, ax = plt.subplots(1,figsize = (10,10))
plt.scatter(-1,0)
plt.plot(re_CL,im_CL)
ax.set_xlim(-3,3)
ax.set_ylim(-3,3)
plt.show()

``````
``````

``````
``````

In [11]:

G = cn.tf(1,[1,1])
c, b = alg.AMIGO_Tune(1,1,0)
KY = cn.tf(c[0],1)+cn.tf(c[1],[1,0])
KR = cn.tf(c[0],1)+cn.tf(c[1],[1,0])
CL = G*KR/(1+G*KY)
y,t = cn.step(CL)

``````
``````

In [12]:

plt.plot(t,y)
plt.show()

``````
``````

In [ ]:

``````