Transcription of State Space Models with Python - halvorsen.blog
1 Hans-Petter Space Models with Textbook with lots of Practical ExamplesAdditional Python Introduction to Control Systems State - Space Models State - Space Models are very useful in Control Theory and Design Python Examples SciPy ( ) The Python Control Systems Library ContentsIt is recommended that you know about Vectors, Matrices and Linear Algebra. If not, take a closer look at my Tutorial Linear Algebra with Python . You should also know about differential equations, see Differential Equations in Python Control SystemControllerProcessSensorsActuatorsF iltering reference ValueControl SignalNoise/DisturbanceProcess Output The different blocks in the Control System can be, , described as a Transfer Function or a State Space Model Control System reference Value, SP (Set-point), SV (Set Value) Measurement Value (MV), Process Value (PV) Error between the reference value and the measurement value ( = ) Disturbance, makes it more complicated to control the process -Control Signal from the ControllerHans-Petter Space ModelsState- Space Models = + = + A general State - Space Model is given by: InputOutputInternal StatesSystemA State - Space model is a structured form or representation of a set of differential equations.
2 State - Space Models are very useful in Control theory and design. The differential equations are converted in matrices and that is the same as !"!# , , and are matrices , , , are vectorsState- Space ModelsAssume we have the following linear equations: != !! !+ "! "+ + #! #+ !! !+ "! "+ + #! # #= !$ !+ "$ "+ + #$ #+ !$ !+ "$ "+ + #! # ! " #= !! #! !$ #$ ! " #+ !! #! !$ #$ ! " # ! " #= !! #! !$ #$ ! " #+ !! #! !$ #$ ! " #We can set the system on matrix/vector form: State - Space ModelsThis gives the following compact form of a general linear State - Space model: ! " #= !! #! !$ #$ ! " #+ !! #! !$ #$ ! " # ! " #= !! #! !$ #$ ! " #+ !! #! !$ #$ ! " # = + = + Where , , and are matricesExample != " "= "+ = !Given the following System:This gives the following State - Space Model: ! "=010 1 ! "+01 =10 ! " =010 1 =01 =10 =0 ExampleGiven the following System:This gives the following State - Space Model: !
3 = "2 "= 2 ! 6 "+4 !+8 " =5 !+6 "+7 ! != " "= ! 3 "+2 !+4 " =5 !+6 "+7 ! ! "=01 1 3 ! "+0024 ! " =56 ! "+70 ! "We can reformulate: =01 1 3 =0024 =56 =70 Example <=2 <+3 =+7 < >=4 <+5 > ==8 = <=6 = >=3 <+3 =+7 <Given the following System:This gives the following State - Space Model: $ % &=203400008 $ % &+700500 $ % $ %=006303 $ % &+0070 $ % =203400008 =700500 =006303 =0070 Hans-Petter ExamplesPython Examples SciPy ( ) Included with Anaconda Distribution Limited functions and Features for Control Systems Python Control Systems Library I will refer to it as the Control Library Very similar features as the matlab Control System Toolbox You need to install it ( pip install control ) You can create, manipulate and simulate State Space Models with both these Python LibrariesHans-Petter The Signal Processing functions SciPy is also included with the Anaconda distribution If you have installed Python using the Anaconda distribution, you don t need to install anything Exampleimport signalA = [[0, 1], [0, -1]]B = [[0], [1]]C = [[1, 0]]D = 0sys = (A, B, C, D) < >=010 1 < >+01 =10 < > State - Space Model: Python Code.
4 Python Exampleimport signalA = [[0, 1], [-1, -3]]B = [[0, 0], [2, 4]]C = [[5, 6]]D = [[7, 0]]sys = (A, B, C, D) ! "=01 1 3 ! "+0024 ! " =56 ! "+70 ! " $ % &=203400008 $ % &+700500 $ % $ %=006303 $ % &+0070 $ %import signalA = ..B = ..C = ..D = ..sys = (A, B, C, D)Step Responseimport sigimport pltimport numpyas np#Simulation Parametersx0 = [0,0]start = 0stop = 30step = 1t = (start,stop,step)K = 3T = 4# State - Space ModelA = [[-1/T, 0], [0, 0]]B = [[K/T], [0]]C = [[1, 0]]D = 0sys = (A, B, C, D)# Step Responset, y = (sys, x0, t)# (t, y) ("Step Response") ("t") ("y") () () ! "= 1 000 ! "+ 0 =10 ! " !=1 !+ "=0We have the differential equations:The State - Space Model becomes:t, y = (sys, x0, t)Here we use the following sigimport pltimport numpyas np#Simulation Parametersx0 = [0,0]start = 0stop = 30step = 1t = (start,stop,step)N = len(t)u = (N)K = 3T = 4# State - Space ModelA = [[-1/T, 0], [0, 0]]B = [[K/T], [0]]C = [[1, 0]]D = 0sys = (A, B, C, D)# Step Responset, y, x = (sys, u, t)# (1) (t, y) ("Step Response") ("t") ("y") () ()# Alternativelyyou can plot one or more of the x variablesx1 = x[:, 0]x2 = x[:, 1] (2) (t, x1, t, x2) ("Step Response") ("t") ("x1 and x2") () () !
5 "= 1 000 ! "+ 0 =10 ! " !=1 !+ "=0We have the differential equations:The State - Space Model becomes:t, y, x = (sys, u, t, x0)Here we use the following function:ResultsPlotting Plotting !and "Hans-Petter Control Systems Library The Python Control Systems Library (control) is a Python package that implements basic operations for analysis and design of feedback control systems. Existing matlab user? The functions and the features are very similar to the matlab Control Systems Toolbox. Python Control Systems Library Homepage: Python Control Systems Library Documentation: Control Systems LibraryThe Python Control Systems Library package may be installed using pip:Installationpip install control PIP is a Package Manager for Python packages/modules. You find more information here: Search for control . The Python Package Index (PyPI) is a repository of Python packages where you use PIP in order to install themAnaconda PromptIf you have installed Python with Anaconda Distribution, use the Anaconda Prompt in order to install it (just search for it using the Search field in Windows).
6 Pip install controlpip listCommand Prompt -PIPC:\Users\hansha\AppData\Local\Progra ms\ Python \Python37-32\Scripts\pip install controlpip install controlpip listor Python37_64 for Python 64bitsPython Control Systems Library -FunctionsFunctions for Model Creation and Manipulation: tf()-Create a transfer function system ss()-Create a State Space system c2d()-Return a discrete-time system tf2ss()-Transform a transfer function to a State Space system ss2tf()-Transform a State Space system to a transfer function .. functions for Model Simulations: step_response()-Step response of a linear system ( , a State - Space Model) lsim()-Simulate the output of a linear system ( , a State - Space Model) Python Exampleimport controlA = [[0, 1], [0, -1]]B = [[0], [1]]C = [[1, 0]]D = 0sys = (A, B, C, D) < >=010 1 < >+01 =10 < >Step Responseimport controlimport plt# Define State - Space ModelA = [[0, 1], [-1, -3]]B = [[1], [0]]C = [[5, 6]]D = [[1]]ssmodel= (A, B, C, D)# Step response for the systemt, y = (ssmodel) (t, y) ("Step Response") ("t") ("y") () () < >=01 1 3 < >+10 =56 < >+1 State - Space Model:T,yout= step_response(sys, T, X0)Here we use the following function.
7 This function uses theforced_response()function with the input set to a unit stepStep Responseimport pltimport control# Define State - Space ModelA = [[0, 1], [-1, -3]]B = [[0, 0], [2, 4]]C = [[5, 6]]D = [[7, 0]]ssmodel= (A, B, C, D)print(ssmodel)t, y = (ssmodel) (t, y) $ %=01 1 3 $ %+0024 $ % =56 $ %+70 $ % State - Space Model:Code Not Working!!!!!Note! This is a MISO system (Multiple Input/Single Output). So, the Solution is to split it into 2 systems, one for !and one for ". Se next similar code will work with matlab , and we will get 2 plots, but the Python Control package does not support thatStep Response1 ( !)import pltimport control# Define State - Space ModelA = [[0, 1], [-1, -3]]B = [[0], [2]]C = [[5, 6]]D = [7]ssmodel= (A, B, C, D)print(ssmodel)t, y = (ssmodel) (t, y) ! "=01 1 3 ! "+02 ! =56 ! "+7 ! State - Space Model:x1 = x[0 ,:]x2 = x[1 ,:] (t, x1, t, x2)We can also plot both !and ":Step Response2 ( ")import pltimport control# Define State - Space ModelA = [[0, 1], [-1, -3]]B = [[0], [4]]C = [[5, 6]]D = [0]ssmodel= (A, B, C, D)print(ssmodel)t, y = (ssmodel) (t, y) State - Space Model:x1 = x[0 ,:]x2 = x[1 ,:] (t, x1, t, x2)We can also plot both !
8 And ": ! "=01 1 3 ! "+04 " =56 ! "+0 "Hans-Petter Space Modelsand Transfer FunctionsIt is recommended that you know about Transfer functions . If not, take a closer look at my Tutorial Transfer functions with Python A general Transfer function is on the form: = ( ) ( )Where is the output and is the input and is the Laplace operator ( ) ( )SISO/MIMO SystemsWe have 4 different Types of Systems: SISO Single Input/Single Output MISO Multiple Input/Single Output SIMO Single Input/Multiple Output MIMO Multiple Input/Multiple OutputSISO import signalimport plt# SISO System# Define State - Space ModelA = [[0, 1], [-1, -3]]B = [[1], [0]]C = [[1, 0]]D = [[2]]# Find Transfer Function from u to ynum, den = (A, B, C, D)H = (num, den)print(H)# Step response for the systemt, y = (H) (t, y) ("Step Response H") ("t") ("y") () () / 0=01 1 3 / 0+10 =10 / 0+2 State - Space Model: ( )= ( ) ( )Single Input/Single OutputSciPy LibrarySISO import controlimport plt# SISO System# Define State - Space ModelA = [[0, 1], [-1, -3]]B = [[1], [0]]C = [[1, 0]]D = [[2]]ssmodel= (A, B, C, D)H = (ssmodel)print(H)# Step response for the systemt, y = (H) (t, y) ("Step Response H") ("t") ("y") () () / 0=01 1 3 / 0+10 =10 / 0+2 State - Space Model: ( )= ( ) ( )Single Input/Single OutputControl LibraryMISO / 0=01 1 3 / 0+0024 / 0 =10 / 0 State - Space Model: !
9 ( )= ( ) !( ) ! " "( )= ( ) "( )import signalimport plt# MISO System# Define State - Space ModelA = [[0, 1], [-1, -3]]B = [[0, 0], [2, 4]]C = [[1, 0]]D = [[0, 0]]# Find Transfer Function from u1 to ynum, den = (A, B, C, D, 0)H1 = (num, den)print(H1)# Find Transfer Function from u2 to ynum, den = (A, B, C, D, 1)H2 = (num, den)print(H2)# Step response for the systemt, y = (H1) (t, y)t, y = (H2) (t, y) ("Step Response") ("t") ("y") (["H1", "H2"]) ()Multiple Input/Single OutputSciPy LibraryMISO / 0=01 1 3 / 0+0024 / 0 =10 / 0 State - Space Model: !( )= ( ) !( ) ! " "( )= ( ) "( )import controlimport plt# MISO System# Define State - Space ModelA = [[0, 1], [-1, -3]]B = [[0, 0], [2, 4]]C = [[1, 0]]D = 0ssmodel= (A, B, C, D)H = (ssmodel)print(H)H1 = H[0,0]print(H1)H2 = H[0,1]print(H2)# Step response for the systemt, y = (H1) (t, y)t, y = (H2) (t, y) ("Step Response") ("t") ("y") (["H1", "H2"]) () ()Multiple Input/Single OutputControl LibrarySIMOS tate- Space Model: !
10 ( )= !( ) ( ) "( )= "( ) ( ) " ! / 0=01 1 3 / 0+02 = / 0=1001 / 0import signalimport plt# SIMO System# Define State - Space ModelA = [[0, 1], [-1, -3]]B = [[0], [2]]C = [[1, 0], [0, 1]]D = [[0]]# Find Transfer Function from u to y1C = [[1, 0]]num, den = (A, B, C, D)H1 = (num, den)print(H1)# Find Transfer Function from u to y2C = [[0, 1]]num, den = (A, B, C, D)H2 = (num, den)print(H2)# Step response for the systemt, y = (H1) (t, y)t, y = (H2) (t, y) ("Step Response") ("t") ("y") (["H1", "H2"]) () ()Single Input/Multiple OutputSciPy LibrarySIMOS tate- Space Model: !( )= !( ) ( ) "( )= "( ) ( ) " ! / 0=01 1 3 / 0+02 = / 0=1001 / 0import controlimport plt# SIMO System# Define State - Space ModelA = [[0, 1], [-1, -3]]B = [[0], [2]]C = [[1, 0], [0, 1]]D = 0ssmodel= (A, B, C, D)H = (ssmodel)print(H)H1 = H[0,0]print(H1)H2 = H[1,0]print(H2)# Step response for the systemt, y = (H)y1 = y[0, :]y2 = y[1, :] (t, y1) (t, y2) ("Step Response") ("t") ("y") (["H1", "H2"]) () ()Single Input/Multiple OutputControl LibraryMIMO / 0=01 1 3 / 0+0024 / 0 = / 0=1001 / 0 State - Space Model: !