使用python绘制根轨迹图

     最近在学自动控制原理,发现根轨迹这一张全是绘图的,然而书上教的全是使用matlab进行计算机辅助绘图。但国内对于使用python进行这种绘图的资料基本没有,后来发现python-control包已经将matlab的rlocus封装进去,matlab能做的python也能做。使用python绘制根轨迹图主要使用的是python-control包下的matlab.rlocus函数,具体内容可以参考:https://python-control.readthedocs.io/en/0.8.0/generated/control.matlab.rlocus.html?highlight=rlocus

      官网对rclous函数的介绍:

control.matlab.rlocus
control.matlab.rlocus(sys, kvect=None, xlim=None, ylim=None, plotstr='-', Plot=True, PrintGain=True, grid=False)
Root locus plot

Calculate the root locus by finding the roots of 1+k*TF(s) where TF is self.num(s)/self.den(s) and each k is an element of kvect.

Parameters:	
sys (LTI object) – Linear input/output systems (SISO only, for now)
kvect (list or ndarray, optional) – List of gains to use in computing diagram
xlim (tuple or list, optional) – control of x-axis range, normally with tuple (see matplotlib.axes)
ylim (tuple or list, optional) – control of y-axis range
Plot (boolean, optional (default = True)) – If True, plot root locus diagram.
PrintGain (boolean (default = True)) – If True, report mouse clicks when close to the root-locus branches, calculate gain, damping and print
grid (boolean (default = False)) – If True plot s-plane grid.
Returns:	
rlist (ndarray) – Computed root locations, given as a 2d array
klist (ndarray or list) – Gains used. Same as klist keyword argument if provided.

下面根据介绍来实现绘制根轨迹图的功能:

一、导入必备包(control、matplotlib)

from control import *     #导入control包,没有的可以使用pip install control命令进行安装
from matplotlib import pyplot as plt    #导入matplotlib包进行绘图

二、设定计算的参数

例如:G(S)H(S) = \frac{K}{4S^{3}+4S^{2}+S},那么输入:

sy1 = tf([1],[4,4,1,0])     #将参数按照tf命令的格式赋值给sy1,前一个[]是分子中s的参数,第二个[]是分母s的参数

三、调用rlocus函数进行绘图

rlocus(sy1)
plt.show()

根轨迹图如下

完整代码

from control import *
from matplotlib import pyplot as plt

sy1 = tf([1],[4,4,1,0])

rlocus(sy1)
plt.show()

猜你喜欢

转载自blog.csdn.net/kingyuan666/article/details/83418230