from control.matlab import *
import matplotlib.pyplot as plt
import numpy as np
import function as func

fig, ax = plt.subplots()
LS = func.linestyle_generator()

# zeta = [1, 0.7, 0.4]
zeta = [0.1, 0, -0.05]
omega_n = 5

# zeta, omega_n = 0.4, 5 #setting dumping coefficient and singular angular frequency

# step response of second order lag system
for i in range(len(zeta)):
    P = tf([0, omega_n**2], [1, 2 * zeta[i] * omega_n, omega_n**2])
    y, t = step(P, np.arange(0, 5, 0.01))

    pltargs = {'ls': next(LS)}
    pltargs['label'] = '$\zeta$ = ' + str(zeta[i])
    ax.plot(t, y, **pltargs)

func.plot_set(ax, 't', 'y', 'best')
plt.show()
Exemplo n.º 2
0
from control.matlab import *
import matplotlib.pyplot as plt
import numpy as np
import function as func

T, K = 0.5, 1  # set time constant and gain
P = tf([0, K], [T, 1])  # 1st order lag system
y, t = step(P, np.arange(0, 5, 0.01))  # step response

fig, ax = plt.subplots()
ax.plot(t, y)
func.plot_set(ax, 't', 'y')
plt.show()
from control.matlab import *
import matplotlib.pyplot as plt
import numpy as np
import function as func
import sympy as sp

sp.init_printing()
s = sp.Symbol('s')
t = sp.Symbol('t', positive=True)

fig, ax = plt.subplots(1, 2)
LS = func.linestyle_generator()

P1 = sp.expand(((s + 3) / ((s + 1) * (s + 2))), s)
P1 = tf([1, 3], [1, 3, 2])
P2 = tf([0, 1], [1, 2, 2, 1])
# print(P2)

y1, t = step(P1, np.arange(0, 10, 0.01))
y2, t = step(P2, np.arange(0, 10, 0.01))

ax[0].plot(t, y1)
ax[1].plot(t, y2)
func.plot_set(ax[0], 't', 'y_1')
func.plot_set(ax[1], 't', 'y_2')
plt.show()
Exemplo n.º 4
0
import matplotlib.pyplot as plt
import numpy as np
import sympy as sp
import scipy
import function as func

w = 1.5  # setting for step size
Y, X = np.mgrid[-w:w:100j, -w:w:100j]

A = np.array([[0, 1], [-4, 5]])
s, v = np.linalg.eig(A)  # eigen vector v and eigen value s for matrix A
print(s)

# calculate components of \dot{x} by using \dot{x} = Ax
U = A[0, 0] * X + A[0, 1] * Y
V = A[1, 0] * X + A[1, 1] * Y

t = np.arange(-1.5, 1.5, 0.01)

fig, ax = plt.subplots()

# plot invariant set only when eigen value of A is a real number
if s.imag[0] == 0 and s.imag[1] == 0:
    ax.plot(t, (v[1, 0] / v[0, 0]) * t, ls='-')
    ax.plot(t, (v[1, 1] / v[0, 1]) * t, ls='-')

# plot phase plane diagram for x
ax.streamplot(X, Y, U, V, density=0.7, color='k')
func.plot_set(ax, '$x_1$', '$x_2$')
plt.show()
from control.matlab import *
import matplotlib.pyplot as plt
import numpy as np
import function as func
import sympy as sp

fig, ax = plt.subplots(2, 2)

# 2nd order lag system
zeta = 0.7
omega_n = 5
P = tf([0, omega_n**2], [1, 2 * zeta * omega_n, omega_n**2])

freq = [2, 5, 10, 20]  # change freqency of input
Td = np.arange(0, 5, 0.01)
for i in range(2):
    for j in range(2):
        u = np.sin(freq[i + j] * Td)  # sin input
        y, t, X0 = lsim(P, u, Td, 0)

        ax[i, j].plot(t, u, ls='--', label='u')
        ax[i, j].plot(t, y, label='y')
        func.plot_set(ax[i, j], 't', 'u, y')

# func.plot_set(ax[0, 0], 't', 'u, y', 'best')
# func.plot_set(ax[0, 1], 't', 'u, y', 'best')
# func.plot_set(ax[1, 0], 't', 'u, y', 'best')
# func.plot_set(ax[1, 1], 't', 'u, y', 'best')
ax[0, 0].legend()
plt.show()
Exemplo n.º 6
0
from control.matlab import *
import matplotlib.pyplot as plt
import numpy as np
import function as func

fig, ax = plt.subplots()
LS = func.linestyle_generator()

K = [1, 2, 3]
T = 0.5
# T = (1, 0.5, 0.1)
# T = -1

for i in range(len(K)):
    y, t = step(tf([0, K[i]], [T, 1]), np.arange(0, 5, 0.01))
    ax.plot(t, y, ls=next(LS), label='K = ' + str(K[i]))

func.plot_set(ax, 't', 'y', 'upper left')
plt.show()

# T, K = 0.5, 1 #set time constant and gain
# P = tf([0, K], [T, 1]) # 1st order lag system
# y, t = step(P, np.arange(0, 5, 0.01)) # step response
import scipy
import function as func

A = [[0, 1], [-4, -5]]
B = [[0], [1]]
C = np.eye(2)  # identity matrix of 2*2
D = np.zeros([2, 1])  # zero matrix of 2*1
P = ss(A, B, C, D)

Td = np.arange(0, 5, 0.01)
Ud = 3 * np.sin(5 * Td)
zero_state = [0, 0]
# X0 = [-0.3, 0.4]
X0 = [0.5, 1]

# xst, t = step(P, Td)  # step response (zero-state response)
xst, t, _ = lsim(P, Ud, Td, zero_state)  # step response (zero-state response)
xin, _ = initial(P, Td, X0)  # zero-input response
x, _, _ = lsim(P, Ud, Td, X0)

fig, ax = plt.subplots(1, 2, figsize=(6, 2.3))
for i in [0, 1]:
    ax[i].plot(t, x[:, i], label='response')
    ax[i].plot(t, xst[:, i], ls='--', label='zero_state')
    ax[i].plot(t, xin[:, i], ls='-.', label='zero_input')

func.plot_set(ax[0], 't', '$x_1$', 'best')
func.plot_set(ax[1], 't', '$x_2$')
fig.tight_layout()
plt.show()