Skip to main content

Control Systems in Python - Part 1 - Bode and Step Response

I hate matlab with passion, yet sadly, nearly everyone uses it.  I'm a fan of Python and open source stuff so here's a simple article on how to do some common control systems stuff in Python.

First we need to make sure the environment is setup.

  1. Install IPython (or you can use any other python shell, but a unicode supported shell is preferred)
  2. Install python-control (numpy, scipy)
  3. Install sympy

These should do if your on Ubuntu/debian:

sudo apt-get install python-sympy python-numpy python-scipy python-matplotlib ipython

Then you need to install python control, see How to download and install python-control

Intro to using Sympy

Open ipython and run the following:

import sympy
from sympy import *
s = Symbol('s')

Now we can do things like define transfer functions using the symbolic variable s.

We can expand the bottom using the .simplify() method

and we can do something more complex like...
which is really nice because it does all the multiplication for us... and it’s much prettier than matlab.  

An example of a system in feedback form:

Note: Use floats (number including a decimal )for all values you insert!

Doing Step Response Plots:
Okay cool, now let’s do something useful, add the following imports:

from control import matlab
import matplotlib.pyplot as plt

Copy or save and import this helper function (use %paste in iPython to enter it)

def stepResponse(Ts,*args,**kwargs):
   num = Poly(Ts.as_numer_denom()[0],s).all_coeffs()
   den = Poly(Ts.as_numer_denom()[1],s).all_coeffs()
   tf =,num),map(float,den))
   y,t = matlab.step([tf],*args,**kwargs)
   plt.title("Step Response")
   plt.xlabel("time (s)")
   info ="OS: %f%s"%(round((y.max()/y[-1]-1)*100,2),'%')
       i10 = next(i for i in range(0,len(y)-1) if y[i]>=y[-1]*.10)
       Tr = round(t[next(i for i in range(i10,len(y)-1) if y[i]>=y[-1]*.90)]-t[i10],2)
   except StopIteration:
       Tr = "unknown"
       Ts = round(t[next(len(y)-i for i in range(2,len(y)-1) if abs(y[-i]/y[-1])>1.02)]-t[0],2)
   except StopIteration:
       Ts = "unknown"
   info += "\nTr: %s"%(Tr)
   info +="\nTs: %s"%(Ts)
   print info

Then run stepResponse(G), where Ts is the transfer function of the system

Doing Bode, Magnitude / Phase plots:

def freqResponse(Ts,*args,**kwargs):
   num = Poly(Ts.as_numer_denom()[0],s).all_coeffs()
   den = Poly(Ts.as_numer_denom()[1],s).all_coeffs()
   tf =,num),map(float,den))

Then you just call freqResponse(ts) like:
You can also pass in arguments to the matlab.bode and matplotlib semilogx methods like:
which plots using dB and Hz scales.  You might notice this is pretty sketch looking, it actually missed a significant part of the magnitude plot, to smooth it out pass in your own omega values, like:
which looks much better.  

There’s lot more things you can do with the python-control library that isn’t implemented in the functions given here (such as plotting for various K values):

Anyways that’s enough for now...

Good luck and have fun!


  1. Hi Jairus,

    Thanks for these posts with control systems + Python. I've been looking for an alternative to MATLAB to help confirm homework answers and this looks like it'll do well.

    I had an issue initially with your stepResponse function - it seems the following line is invalid:

    y,t = matlab.step([tf],*args,**kwargs)

    I've removed the list for the tf argument and I'm now able to use the function. With the list matlab.step() doesn't return the expected yout/time lists with version 0.6.

    Perhaps this is due to differing versions or something of that nature.


  2. This comment has been removed by the author.

  3. OVERSHOOT, round(..., 2) but why show more digits in the legend?

  4. yout, t = matlab.step(tf, *args, **kwargs) #should have no [ ] or?

  5. Same I have tried with tf without [ ] and *args and **kwargs

  6. Thanks for this post...and the others. !!!!!! Cheers

  7. My code have it errors when i copy and paste. Would like to have a direct chat with you cos I'm trying to figure this out with my Lecturer and it will be great to finally get a solution

  8. Thanks for providing good information,Thanks for your sharing python Online Training


Post a Comment

Popular posts from this blog

Kivy vs React-Native for building cross platform mobile apps

I've built three apps now using Kivy and one with React-Native, just wanted to share my thoughts on both.

Just a warning, I am strongly biased towards python and this is all based on opinion and experience and is thus worth what you pay for it. I don't claim to be an expert in either of these, just have worked with each for several months.  If something is incorrect I'd love to hear advice.

Demo of one of the apps

Nice to be able to run natively on the desktop WITHOUT a simulatorPython is easy to work withUse (almost) any python libraryVery easy to create custom widgetsKivy properties and data binding just work. Way nicer than React's "state" / flux / redux whatever you want to call it (stupid?). Native interfaces (pyjnius) and (pyobjc)Runs and feels pretty smooth Cons:Default widget toolkit looks like Android 4.4. Requiring you use your own widgets or a theming kit like KivyMD if styling bothers youCreating dynamic widgets declaratively is not yet s…