Balanced model reduction examples

Code

 1#!/usr/bin/env python
 2
 3import os
 4
 5import numpy as np
 6import control.modelsimp as msimp
 7import control.matlab as mt
 8from control.statesp import StateSpace
 9import matplotlib.pyplot as plt
10
11plt.close('all')
12
13# controllable canonical realization computed in MATLAB for the
14# transfer function: num = [1 11 45 32], den = [1 15 60 200 60]
15A = np.array([
16    [-15., -7.5, -6.25, -1.875],
17    [8., 0., 0., 0.],
18    [0., 4., 0., 0.],
19    [0., 0., 1., 0.]
20])
21B = np.array([
22    [2.],
23    [0.],
24    [0.],
25    [0.]
26])
27C = np.array([[0.5, 0.6875, 0.7031, 0.5]])
28D = np.array([[0.]])
29
30# The full system
31fsys = StateSpace(A, B, C, D)
32
33# The reduced system, truncating the order by 1
34n = 3
35rsys = msimp.balred(fsys, n, method='truncate')
36
37# Comparison of the step responses of the full and reduced systems
38plt.figure(1)
39y, t = mt.step(fsys)
40yr, tr = mt.step(rsys)
41plt.plot(t.T, y.T)
42plt.plot(tr.T, yr.T)
43
44# Repeat balanced reduction, now with 100-dimensional random state space
45sysrand = mt.rss(100, 1, 1)
46rsysrand = msimp.balred(sysrand, 10, method='truncate')
47
48# Comparison of the impulse responses of the full and reduced random systems
49plt.figure(2)
50yrand, trand = mt.impulse(sysrand)
51yrandr, trandr = mt.impulse(rsysrand)
52plt.plot(trand.T, yrand.T, trandr.T, yrandr.T)
53
54if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
55    plt.show()

Notes

1. The environment variable PYCONTROL_TEST_EXAMPLES is used for testing to turn off plotting of the outputs.