使用python-control库实现MATLAB自动控制原理常用函数:Bode图 Nyquist图 根轨迹

最近在学习自动控制原理,电脑装的windows/ubuntu双系统,matlab安装在windows上,日常用ubuntu,写作业切换系统过于繁琐,因此想要找到python中可以替换MATLAB Control工具箱的函数库,尝试了scipy.signal、自行编写bode、nyquist等函数方法后,找到了python control库,非常方便,特此记录。Control官方网站。

安装

# pip安装
pip install slycot   # 可选
pip install control

# conda安装
conda install numpy scipy matplotlib    # 如果没有安装过这几个库,否则不用这行这一句
conda install -c conda-forge control

# 测试是否安装成功
python -c "import control" # 如果没有报错说明安装成功

功能

状态方程

生成状态方程
G ( s ) = 25 s 2 + 4 s + 25 G(s)=\frac{25}{s^2+4s+25} G(s)=s2+4s+2525

import control as ctr
import matplotlib.pyplot as plt

# 使用tf生成状态方程
sys = ctr.tf([25],[1,4,25])

# 使用表达式生成状态方程
s = ctr.tf('s')
sys  = 25.0/(s**2+4.0*s+25.0)

Bode图

plt.figure(figsize=(12,12)) # 调整图像大小
ctr.bode_plot(sys)

使用python-control库实现MATLAB自动控制原理常用函数:Bode图 Nyquist图 根轨迹_第1张图片

Nyquist图

ctr.nyquist_plot(sys)

使用python-control库实现MATLAB自动控制原理常用函数:Bode图 Nyquist图 根轨迹_第2张图片

根轨迹图

ctr.rlocus(sys)

使用python-control库实现MATLAB自动控制原理常用函数:Bode图 Nyquist图 根轨迹_第3张图片

LQR控制器

A = [[0,1,0,0],[0,0,-25,0],[0,0,0,1],[0,0,7,0]]
B = [[0],[0.25],[0],[-0.5]]
Q = [[1,0,0,0],[0,1,0,0],[0,0,100,0],[0,0,0,100]]
R = 1
[K,S,e] = ctr.lqr(A,B,Q,R)
print("K:\n",K)
print("S:\n",S)
print("e:\n",e)

输出

K:
[[  1.           2.25099079 -57.7198152  -17.12633357]]
S:
[[  2.25099079   2.03347978 -17.12633357  -0.98326011]
 [  2.03347978   3.7807706  -37.56795909  -2.61159629]
 [-17.12633357 -37.56795909 692.61081876  96.65565085]
 [ -0.98326011  -2.61159629  96.65565085  32.94686901]]
e:
[-6.1071997+0.j	-1.1592202+0.j	-0.9297473+0.8087148j	-0.9297473-0.8087148j]

你可能感兴趣的:(python,matlab,控制器,信号处理)