Skip to content

Add H2 and Hinf synthesis examples #895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ other sources.
phaseplots
robust_siso
robust_mimo
scherer_etal_ex7_H2_h2syn
scherer_etal_ex7_Hinf_hinfsyn
cruise-control
steering-gainsched
steering-optimal
Expand Down
1 change: 1 addition & 0 deletions doc/scherer_etal_ex7_H2_h2syn.py
15 changes: 15 additions & 0 deletions doc/scherer_etal_ex7_H2_h2syn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
H2 synthesis, based on Scherer et al. 1997 example 7
----------------------------------------------------

Code
....
.. literalinclude:: scherer_etal_ex7_H2_h2syn.py
:language: python
:linenos:


Notes
.....

1. The environment variable `PYCONTROL_TEST_EXAMPLES` is used for
testing to turn off plotting of the outputs.
1 change: 1 addition & 0 deletions doc/scherer_etal_ex7_Hinf_hinfsyn.py
15 changes: 15 additions & 0 deletions doc/scherer_etal_ex7_Hinf_hinfsyn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Hinf synthesis, based on Scherer et al. 1997 example 7
------------------------------------------------------

Code
....
.. literalinclude:: scherer_etal_ex7_Hinf_hinfsyn.py
:language: python
:linenos:


Notes
.....

1. The environment variable `PYCONTROL_TEST_EXAMPLES` is used for
testing to turn off plotting of the outputs.
84 changes: 84 additions & 0 deletions examples/scherer_etal_ex7_H2_h2syn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""H2 design using h2syn.

Demonstrate H2 desing for a SISO plant using h2syn. Based on [1], Ex. 7.

[1] Scherer, Gahinet, & Chilali, "Multiobjective Output-Feedback Control via
LMI Optimization", IEEE Trans. Automatic Control, Vol. 42, No. 7, July 1997.

[2] Zhou & Doyle, "Essentials of Robust Control", Prentice Hall,
Upper Saddle River, NJ, 1998.
"""
# %%
# Packages
import numpy as np
import control

# %%
# State-space system.

# Process model.
A = np.array([[0, 10, 2],
[-1, 1, 0],
[0, 2, -5]])
B1 = np.array([[1],
[0],
[1]])
B2 = np.array([[0],
[1],
[0]])

# Plant output.
C2 = np.array([[0, 1, 0]])
D21 = np.array([[2]])
D22 = np.array([[0]])

# H2 performance.
C1 = np.array([[0, 1, 0],
[0, 0, 1],
[0, 0, 0]])
D11 = np.array([[0],
[0],
[0]])
D12 = np.array([[0],
[0],
[1]])

# Dimensions.
n_u, n_y = 1, 1

# %%
# H2 design using h2syn.

# Create augmented plant.
Baug = np.block([B1, B2])
Caug = np.block([[C1], [C2]])
Daug = np.block([[D11, D12], [D21, D22]])
Paug = control.ss(A, Baug, Caug, Daug)

# Input to h2syn is Paug, number of inputs to controller,
# and number of outputs from the controller.
K = control.h2syn(Paug, n_y, n_u)

# Extarct controller ss realization.
A_K, B_K, C_K, D_K = K.A, K.B, K.C, K.D

# %%
# Compute closed-loop H2 norm.

# Compute closed-loop system, Tzw(s). See Eq. 4 in [1].
Azw = np.block([[A + B2 @ D_K @ C2, B2 @ C_K],
[B_K @ C2, A_K]])
Bzw = np.block([[B1 + B2 @ D_K @ D21],
[B_K @ D21]])
Czw = np.block([C1 + D12 @ D_K @ C2, D12 @ C_K])
Dzw = D11 + D12 @ D_K @ D21
Tzw = control.ss(Azw, Bzw, Czw, Dzw)

# Compute closed-loop H2 norm via Lyapunov equation.
# See [2], Lemma 4.4, pg 53.
Qzw = control.lyap(Azw.T, Czw.T @ Czw)
nu = np.sqrt(np.trace(Bzw.T @ Qzw @ Bzw))
print(f'The closed-loop H_2 norm of Tzw(s) is {nu}.')
# Value is 7.748350599360575, the same as reported in [1].

# %%
57 changes: 57 additions & 0 deletions examples/scherer_etal_ex7_Hinf_hinfsyn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Hinf design using hinfsyn.

Demonstrate Hinf desing for a SISO plant using h2syn. Based on [1], Ex. 7.

[1] Scherer, Gahinet, & Chilali, "Multiobjective Output-Feedback Control via
LMI Optimization", IEEE Trans. Automatic Control, Vol. 42, No. 7, July 1997.
"""
# %%
# Packages
import numpy as np
import control

# %%
# State-space system.

# Process model.
A = np.array([[0, 10, 2],
[-1, 1, 0],
[0, 2, -5]])
B1 = np.array([[1],
[0],
[1]])
B2 = np.array([[0],
[1],
[0]])

# Plant output.
C2 = np.array([[0, 1, 0]])
D21 = np.array([[2]])
D22 = np.array([[0]])

# Hinf performance.
C1 = np.array([[1, 0, 0],
[0, 0, 0]])
D11 = np.array([[0],
[0]])
D12 = np.array([[0],
[1]])

# Dimensions.
n_x, n_u, n_y = 3, 1, 1

# %%
# Hinf design using hinfsyn.

# Create augmented plant.
Baug = np.block([B1, B2])
Caug = np.block([[C1], [C2]])
Daug = np.block([[D11, D12], [D21, D22]])
Paug = control.ss(A, Baug, Caug, Daug)

# Input to hinfsyn is Paug, number of inputs to controller,
# and number of outputs from the controller.
K, Tzw, gamma, rcond = control.hinfsyn(Paug, n_y, n_u)
print(f'The closed-loop H_inf norm of Tzw(s) is {gamma}.')

# %%