A short Python tutorial¶
Note
If you are already familiar with Python, you can skip this section. All you need to know is that the matrices describing the MPC setup can be entered as a list of lists, or as 2-dimensional NumPy arrays or NumPy matrices.
Although Python knowledge is not required to use this tool, there are a few things that users not familiar with Python should know before going into next sections:
- white spaces count,
- Python lists help build matrices,
- NumPy provides some MATLAB-like functionality, and
- array indices start at 0.
A detailed explanation of each item follows.
White spaces¶
When writing the system module in the following sections, make sure there are no white spaces (tabs, spaces, etc.) at the beginning of each line.
Python lists¶
Python lists are simply a collection of objects separated by commas within squared brackets. Matrices and vectors are entered as a list of numeric lists. For example, a 2x2 identity matrix is entered as:
I = [[1, 0], [0, 1]]
whereas a 2x1 column vector is entered as:
c = [[5], [5]]
Strictly speaking, I
and c
are not really matrices,
but they are internally converted to such by the ltidt
module.
NumPy¶
At the top of the system module, you can write:
from numpy import *
This makes available some functions similar to MATLAB. Of interest are
diag
, eye
, zeros
, ones
. For example, a 2x2
identity matrix can also be entered as:
I = eye(2)
or:
I = diag([1, 1])
The zeros
and ones
commands have a special notation,
as they require the size of the matrix as a list. For example,
the c
column vector from above can be written for example as:
c = 5 * ones([2, 1])
Indexing¶
Finally, a few remark on indexing. We could also create the 2x2 identity matrix as follows:
I = zeros([2, 2])
I[0, 0] = 1
I[1, 1] = 1
Note that indexing starts at 0. Slicing rules are similar to those of MATLAB.
More information¶
For more details see the Python tutorial, and the NumPy for MATLAB users tutorial.