test_lyapunov_discrete.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
26 
27 #include <corbo-core/macros.h>
28 
29 #include "gtest/gtest.h"
30 
32 
33 class TestLyapunovEquationDiscreteTime : public testing::Test
34 {
35  protected:
36  // You can do set-up work for each test here.
38  // You can do clean-up work that doesn't throw exceptions here.
40  // If the constructor and destructor are not enough for setting up
41  // and cleaning up each test, you can define the following methods:
42 
43  // Code here will be called immediately after the constructor (right
44  // before each test).
45  // virtual void SetUp() {}
46  // Code here will be called immediately after each test (right
47  // before the destructor).
48  // virtual void TearDown();
49 };
50 
52 {
53  Eigen::Matrix2d A;
54  A << 0.2, 1, 0, -0.25;
55 
56  Eigen::Matrix2d Q;
57  Q << 2, 0, 0, 2;
58 
59  EXPECT_TRUE(LyapunovDiscrete::hasUniqueSolution(A));
60 
61  Eigen::MatrixXd X;
62  bool solve_success = LyapunovDiscrete::solve(A, Q, X);
63  EXPECT_TRUE(solve_success);
64 
65  Eigen::Matrix2d X_sol;
66  X_sol << 4.0939153, -0.5079365, -0.5079365, 2.133333;
67 
68  EXPECT_EQ_MATRIX(X, X_sol, 1e-5);
69 }
70 
72 {
73  // G(z) = 1/(z+0.9)/(z+0.7)/(z+0.5)/(z+0.3)/(z+0.1)
74  Eigen::MatrixXd A(5, 5);
75  A << -2.5, -2.3, -0.95, -0.1689, -0.0095, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0;
76 
77  Eigen::MatrixXd Q(5, 5);
78  // Q = magic(5)/100
79  Q << 0.17, 0.24, 0.01, 0.08, 0.15, 0.23, 0.05, 0.07, 0.14, 0.16, 0.04, 0.06, 0.13, 0.20, 0.22, 0.10, 0.12, 0.19, 0.21, 0.03, 0.11, 0.18, 0.25,
80  0.02, 0.09;
81 
82  EXPECT_TRUE(LyapunovDiscrete::hasUniqueSolution(A));
83 
84  Eigen::MatrixXd X;
85  bool solve_success = LyapunovDiscrete::solve(A, Q, X);
86  EXPECT_TRUE(solve_success);
87 
88  Eigen::MatrixXd X_sol(5, 5);
89  X_sol << 617.5979, -611.0219, 592.5646, -565.5248, 532.3784, -610.8914, 617.6479, -610.9519, 592.7046, -565.3648, 592.3457, -610.8314, 617.7779,
90  -610.7519, 592.9246, -565.2179, 592.4657, -610.6414, 617.9879, -610.7219, 532.0166, -565.0379, 592.7157, -610.6214, 618.0779;
91 
92  EXPECT_EQ_MATRIX(X, X_sol, 1e-5);
93 }
94 
96 {
97  // this test differs from solve_feasible 1-2 such that
98  // the underlying Schur solver in the current implementation must be
99  // of complex type (rather than real) in order to find the correct solution.
100  Eigen::MatrixXd A(5, 5);
101  A << -2.5, -2.3, -0.95, -0.1689, -0.095, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0;
102 
103  Eigen::MatrixXd Q = Eigen::MatrixXd::Identity(5, 5);
104 
105  EXPECT_TRUE(LyapunovDiscrete::hasUniqueSolution(A));
106 
107  Eigen::MatrixXd X;
108  bool solve_success = LyapunovDiscrete::solve(A, Q, X);
109  EXPECT_TRUE(solve_success);
110 
111  Eigen::MatrixXd X_sol(5, 5);
112  X_sol << -0.9387, 11.8081, -41.2126, 76.7855, -109.8624, 11.8081, 0.0613, 11.8081, -41.2126, 76.7855, -41.2126, 11.8081, 1.0613, 11.8081,
113  -41.2126, 76.7855, -41.2126, 11.8081, 2.0613, 11.8081, -109.8624, 76.7855, -41.2126, 11.8081, 3.0613;
114 
115  EXPECT_EQ_MATRIX(X, X_sol, 1e-5);
116 }
117 
119 {
120  Eigen::MatrixXd A = Eigen::MatrixXd::Random(5, 4);
121 
122  Eigen::Matrix2d Q;
123  Q << 3, 1, 1, 1;
124 
125  Eigen::MatrixXd X;
126  EXPECT_DEBUG_DEATH(LyapunovDiscrete::solve(A, Q, X), "");
127 }
128 
130 {
131  Eigen::MatrixXd A = Eigen::MatrixXd::Random(4, 4);
132  Eigen::MatrixXd Q = Eigen::MatrixXd::Random(5, 4);
133 
134  Eigen::MatrixXd X;
135  EXPECT_DEBUG_DEATH(LyapunovDiscrete::solve(A, Q, X), "");
136 }
137 
138 TEST_F(TestLyapunovEquationDiscreteTime, try_solve_A_Q_dim_mismatch)
139 {
140  Eigen::MatrixXd A = Eigen::MatrixXd::Random(4, 4);
141  Eigen::MatrixXd Q = Eigen::MatrixXd::Random(5, 5);
142 
143  Eigen::MatrixXd X;
144  EXPECT_DEBUG_DEATH(LyapunovDiscrete::solve(A, Q, X), "");
145 }
146 
147 TEST_F(TestLyapunovEquationDiscreteTime, has_unique_solution_non_square)
148 {
149  Eigen::MatrixXd A = Eigen::MatrixXd::Random(5, 4);
150 
151  Eigen::MatrixXd X;
152  EXPECT_FALSE(LyapunovDiscrete::hasUniqueSolution(A));
153 }
154 
155 TEST_F(TestLyapunovEquationDiscreteTime, has_unique_solution_infeasible1)
156 {
157  Eigen::Matrix3d A = Eigen::Matrix3d::Identity();
158 
159  EXPECT_FALSE(LyapunovDiscrete::hasUniqueSolution(A));
160 }
TestLyapunovEquationDiscreteTime
Definition: test_lyapunov_discrete.cpp:33
macros.h
X
#define X
Definition: icosphere.cpp:20
EXPECT_EQ_MATRIX
#define EXPECT_EQ_MATRIX(A, B, tol)
Definition: macros.h:61
lyapunov_discrete.h
TEST_F
TEST_F(TestLyapunovEquationDiscreteTime, solve_feasible1)
Definition: test_lyapunov_discrete.cpp:51
TestLyapunovEquationDiscreteTime::TestLyapunovEquationDiscreteTime
TestLyapunovEquationDiscreteTime()
Definition: test_lyapunov_discrete.cpp:37
A
MatrixType A(a, *n, *n, *lda)
corbo::LyapunovDiscrete
Methods for dealing with discrete-time lyapunov equations.
Definition: lyapunov_discrete.h:80
TestLyapunovEquationDiscreteTime::~TestLyapunovEquationDiscreteTime
virtual ~TestLyapunovEquationDiscreteTime()
Definition: test_lyapunov_discrete.cpp:39


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Wed Mar 2 2022 00:07:06