-
Notifications
You must be signed in to change notification settings - Fork 8
/
ek_care.m
162 lines (129 loc) · 4.15 KB
/
ek_care.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
function [Xu, VA, D, it, res] = ek_care(A, B, u, maxdim, tol, debug, nrmtype, varargin)
%EK_CARE Approximate the solution of the continuous-time algebraic Riccati equation (CARE):
%
% A'X + XA - XBB'X + uu' = 0
%
% [XU, VA, D, it] = EK_CARE(A, B, C, MAXDIM, TOL, DEBUG, 'kernel', K) solves
%
% A' X + X A - X B B' X + u K u' = 0,
%
% where K is a symmetric matrix. It also returns the basis VA, the D such that
% X = XU * D * XU' and the number of iterations needed for convergence. maxdim
% is the maximum dimension allowed for the projection space.
% The optional parameters TOL and DEBUG control the stopping criterion
% and the debugging during the iteration.
if ~exist('debug', 'var')
debug = false;
end
if ~exist('tol', 'var')
tol = 1e-8;
end
if ~exist('nrmtype', 'var')
nrmtype = 2;
end
if size(u, 2) == 0
Xu = u;
VA = u;
D = [];
it = 0;
return;
end
p = inputParser;
addParameter(p, 'kernel', 1);
parse(p, varargin{:});
K = p.Results.kernel;
if ~issymmetric(K)
error('EK_LYAP:: kernel of the RHS is not symmetric')
end
if ~isstruct(A)
if issparse(A)
AA = ek_struct(A', issymmetric(A));
nrmA = normest(A, 1e-2);
else
AA = ek_struct(A', false);
nrmA = norm(A, nrmtype);
end
AA.nrm = nrmA;
else % in this case the structure should refer already to the conjugate transpose of A
AA = A;
nrmA = AA.nrm;
end
% tol can be function tol(r, n) that is given the residual and the norm, or
% a scalar. In the latter case, we turn it into a function
if isfloat(tol)
tol_eps = tol;
tol = @(r, n) r < tol_eps * n;
end
% Dimension of the space
sa = size(u, 2);
bsa = sa;
it = 1;
while sa - 2*bsa < maxdim
if exist('VA', 'var') && ( size(VA, 2) + 2 * bsa >= size(VA, 1) )
na = size(VA, 1);
A = AA.multiply(1.0, 0.0, eye(na));
if ishermitian(K) && eigs(K, 1, 'smallestreal') >= 0 % care and icare work only for positive definite rhs
if ~exist('icare', 'file')
Y = care(A', B, u * K * u');
else
Y = icare(A', B, u * K * u');
end
else
Y = small_care_solve(A', B, u * K * u', hodlroption('threshold'), 50);
end
[QQ, DD] = eig(.5 * (Y + Y'));
switch nrmtype
case 2
s = abs(diag(DD));
rk = sum( arrayfun(@(ss) tol(ss, max(s) / (2 *nrmA)), s) == 0);
case 'fro'
d = sort(abs(diag(DD)));
s = sqrt(cumsum(d.^2));
rk = sum( arrayfun(@(ss) tol(ss, s(end) / (2 * nrmA)), s) == 0 );
end
[~,ii] = sort(diag(abs(DD))); ii = ii(end:-1:end-rk+1);
Xu = VA(:,1:size(QQ,1)) * QQ(:,ii) * sqrt(abs(DD(ii,ii)));
D = diag(sign(diag(DD(ii, ii))));
if debug
fprintf('%d Dense solver: rank %d, size %d\n', it, rk, max(na,nb));
end
return;
end
if ~exist('VA', 'var')
[VA, KA, HA, params] = ek_krylov(AA, u);
else
[VA, KA, HA, params] = ek_krylov(VA, KA, HA, params);
end
sa = size(VA, 2);
% Compute the solution and residual of the projected CARE
As = HA / KA(1:end-bsa,:);
Cs = zeros(size(As, 1), size(As, 1));
if ~exist('Cprojected', 'var')
Cprojected = ( VA(:,1:size(u,2))' * u ) * K * ( u' * VA(:,1:size(u,2)) );
end
Cs(1:size(u,2), 1:size(u,2)) = Cprojected;
Bs = VA(:,1:size(As, 1) - bsa)' * B;
[Y, res] = care_galerkin(As, Bs, Cs, bsa, nrmtype);
% You might want to enable this for debugging purposes
if debug
fprintf('%d Residue: %e\n', it, res / norm(Y, nrmtype));
end
if tol(res, norm(Y, nrmtype)) % res < norm(Y) * tol
break
end
it = it + 1;
end
[QQ, DD] = eig(.5 * (Y + Y'));
switch nrmtype
case 2
s = abs(diag(DD));
rk = sum( arrayfun(@(ss) tol(ss, max(s) / (2 *nrmA)), s) == 0);
case 'fro'
d = sort(abs(diag(DD)));
s = sqrt(cumsum(d.^2));
rk = sum( arrayfun(@(ss) tol(ss, s(end) / (2 * nrmA)), s) == 0 );
end
[~,ii] = sort(diag(abs(DD))); ii = ii(end:-1:end-rk+1);
Xu = VA(:, 1:size(QQ, 1)) * QQ(:, ii) * sqrt(abs(DD(ii, ii)));
D = diag(sign(diag(DD(ii, ii))));
end