From 58e43f18e1fa7d6fcb69d5d8cdce05de2fde042e Mon Sep 17 00:00:00 2001 From: James Forbes Date: Tue, 23 May 2023 11:20:49 -0400 Subject: [PATCH 1/5] Add H2 and Hinf exmaples --- doc/scherer_etal_ex7_H2_h2syn.py | 1 + doc/scherer_etal_ex7_H2_h2syn.rst | 15 ++++ doc/scherer_etal_ex7_Hinf_hinfsyn.rst | 15 ++++ ...l_ex7_Hinfscherer_etal_ex7_Hinf_hinfsyn.py | 1 + examples/scherer_etal_ex7_H2_h2syn.py | 84 +++++++++++++++++++ examples/scherer_etal_ex7_Hinf_hinfsyn.py | 57 +++++++++++++ 6 files changed, 173 insertions(+) create mode 120000 doc/scherer_etal_ex7_H2_h2syn.py create mode 100644 doc/scherer_etal_ex7_H2_h2syn.rst create mode 100644 doc/scherer_etal_ex7_Hinf_hinfsyn.rst create mode 120000 doc/scherer_etal_ex7_Hinfscherer_etal_ex7_Hinf_hinfsyn.py create mode 100644 examples/scherer_etal_ex7_H2_h2syn.py create mode 100644 examples/scherer_etal_ex7_Hinf_hinfsyn.py diff --git a/doc/scherer_etal_ex7_H2_h2syn.py b/doc/scherer_etal_ex7_H2_h2syn.py new file mode 120000 index 000000000..527f80144 --- /dev/null +++ b/doc/scherer_etal_ex7_H2_h2syn.py @@ -0,0 +1 @@ +../examples/scherer_etal_ex7_H2_h2syn.py \ No newline at end of file diff --git a/doc/scherer_etal_ex7_H2_h2syn.rst b/doc/scherer_etal_ex7_H2_h2syn.rst new file mode 100644 index 000000000..7d9eea728 --- /dev/null +++ b/doc/scherer_etal_ex7_H2_h2syn.rst @@ -0,0 +1,15 @@ +H2 synthesis, based on cherer 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. diff --git a/doc/scherer_etal_ex7_Hinf_hinfsyn.rst b/doc/scherer_etal_ex7_Hinf_hinfsyn.rst new file mode 100644 index 000000000..1a2294535 --- /dev/null +++ b/doc/scherer_etal_ex7_Hinf_hinfsyn.rst @@ -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. diff --git a/doc/scherer_etal_ex7_Hinfscherer_etal_ex7_Hinf_hinfsyn.py b/doc/scherer_etal_ex7_Hinfscherer_etal_ex7_Hinf_hinfsyn.py new file mode 120000 index 000000000..7755a325f --- /dev/null +++ b/doc/scherer_etal_ex7_Hinfscherer_etal_ex7_Hinf_hinfsyn.py @@ -0,0 +1 @@ +../examples/scherer_etal_ex7_Hinf_hinfsyn.py \ No newline at end of file diff --git a/examples/scherer_etal_ex7_H2_h2syn.py b/examples/scherer_etal_ex7_H2_h2syn.py new file mode 100644 index 000000000..0b47dd8a1 --- /dev/null +++ b/examples/scherer_etal_ex7_H2_h2syn.py @@ -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]. + +# %% diff --git a/examples/scherer_etal_ex7_Hinf_hinfsyn.py b/examples/scherer_etal_ex7_Hinf_hinfsyn.py new file mode 100644 index 000000000..973181154 --- /dev/null +++ b/examples/scherer_etal_ex7_Hinf_hinfsyn.py @@ -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}.') + +# %% From 093670a85ed1ff7c656b1143b64d7788ca009826 Mon Sep 17 00:00:00 2001 From: James Forbes Date: Tue, 23 May 2023 11:29:50 -0400 Subject: [PATCH 2/5] Add H2 and Hinf examples to example.rst --- doc/examples.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/examples.rst b/doc/examples.rst index 02330f903..505bcf7a3 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -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 From 981411c58fa2459da11902e2f36569d0f566ff69 Mon Sep 17 00:00:00 2001 From: James Forbes Date: Tue, 23 May 2023 11:35:02 -0400 Subject: [PATCH 3/5] Fix filename in Hinf syn link --- ..._etal_ex7_Hinf_hinfsyn.py => scherer_etal_ex7_Hinf_hinfsyn.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename doc/{scherer_etal_ex7_Hinfscherer_etal_ex7_Hinf_hinfsyn.py => scherer_etal_ex7_Hinf_hinfsyn.py} (100%) diff --git a/doc/scherer_etal_ex7_Hinfscherer_etal_ex7_Hinf_hinfsyn.py b/doc/scherer_etal_ex7_Hinf_hinfsyn.py similarity index 100% rename from doc/scherer_etal_ex7_Hinfscherer_etal_ex7_Hinf_hinfsyn.py rename to doc/scherer_etal_ex7_Hinf_hinfsyn.py From 1921f419661e648c31697da3a4cec28340c4a8e6 Mon Sep 17 00:00:00 2001 From: James Forbes Date: Tue, 23 May 2023 11:38:35 -0400 Subject: [PATCH 4/5] Fix spelling mistake in header of H2 example --- doc/scherer_etal_ex7_H2_h2syn.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/scherer_etal_ex7_H2_h2syn.rst b/doc/scherer_etal_ex7_H2_h2syn.rst index 7d9eea728..ab1ff65f1 100644 --- a/doc/scherer_etal_ex7_H2_h2syn.rst +++ b/doc/scherer_etal_ex7_H2_h2syn.rst @@ -1,4 +1,4 @@ -H2 synthesis, based on cherer et al. 1997 example 7 +H2 synthesis, based on Scherer et al. 1997 example 7 --------------------------------------------------- Code From ecc9116a4e77191b086cd4e4ad12c840a5232e79 Mon Sep 17 00:00:00 2001 From: James Forbes Date: Tue, 23 May 2023 11:44:15 -0400 Subject: [PATCH 5/5] Fix underline errore in header of H2 rst file --- doc/scherer_etal_ex7_H2_h2syn.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/scherer_etal_ex7_H2_h2syn.rst b/doc/scherer_etal_ex7_H2_h2syn.rst index ab1ff65f1..ef386be61 100644 --- a/doc/scherer_etal_ex7_H2_h2syn.rst +++ b/doc/scherer_etal_ex7_H2_h2syn.rst @@ -1,5 +1,5 @@ H2 synthesis, based on Scherer et al. 1997 example 7 ---------------------------------------------------- +---------------------------------------------------- Code ....