rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
riccati_gamma.m
Go to the documentation of this file.
1 function gamma = riccati_gamma(model, model_data, sim)
2 % This function calculates gamma.
3 % Simdata must be a high dimensional solution.
4 %
5 % This is done by solving the generalized Lyapunov equation
6 % A' H E + E' H A = - I
7 % and then gamma = || H ||
8 %
9 % Andreas Schmidt, 2015
10 
11 % Build the full dimensional system matrices:
12 [E,A,B,~] = riccati_assemble_system_matrices(model, model_data);
13 
14 if ~isfield(model, 'gamma_mode') || strcmp(model.gamma_mode, 'lyap')
15  Y = A-B*B'*sim.Z*sim.Z'*E;
16  gamma = normest(lyap(Y', -eye(model.n), [], E));
17 elseif strcmp(model.gamma_mode, 'power_iteration')
18  % This function is work in progress!!!!!
19  % Initial guess for the "eigenvector"
20  [x0,~,~] = eigs(A,E,1,'sm');
21  x0 = x0/norm(x0);
22 
23  e0 = norm(x0);
24  tol = 1e-8;
25 
26  Tend = 1e10;
27  Z = sim.Z;
28 
29  % Outer iteration:
30  for l = 1:20
31  % First inner iteration. Integrate the system
32  % E \dotx = (A-BB'PE)x, x(0) = E\x0
33  t = 0;
34 
35  X = zeros(5000, model.n);
36  dt0 = 1e-3;
37  dt = dt0;
38  X(1,:) = x0;
39  norm0 = norm(X(1,:));
40  k = 1;
41  h = [];
42  T = [];
43 
44  M = sparse(diag(diag(E)));
45  while t<Tend
46  rhs = E*X(k,:)';
47 
48  [x,~,~,iter,~] = bicgstab(afun(E,A,B,Z,dt), rhs, tol, 200, [], []);
49 
50  X(k+1, :) = x;
51 
52  t = t + dt;
53 
54  % Adjust dt:
55  tau = x - X(k,:)';
56  dt = dt*0.9*(1e-2/norm(tau))^0.3;
57 
58  T = [T t];
59  k = k + 1;
60  norm(X(k,:))
61  if norm(X(k,:)) < 1e-4
62  break
63  end
64  end
65 
66  % Integrate backwards to get gamma:
67 
68  % Perform the "backward" solution:
69  K = k - 2;
70  y = zeros(model.n, 1);
71 
72  for k = 1:K
73  j = K+1-k;
74  g = X(j,:)';
75  dt = T(j+1) - T(j);
76  rhs = E'*y + dt*g;
77  [y,~,~,~,~] = bicgstab(afun2(E,A,B,Z,dt), rhs, tol, 200, [], [], y);
78  end
79 
80  y = E'\y;
81  gamma = norm(y);
82  display(['Relative change ', num2str(abs(gamma-e0)/e0)])
83  if abs(gamma-e0)/e0 < 1e-3
84  break
85  end
86 
87  %sim2 = riccati_simulate_system(model, model_data, 'use_feedback', sim, 'x0', zeros(model.n,1), 'use_rhs', flip(sim1.x), 'nt', 200, 'T', 10);
88  %y = E'\sim2.x(end,:)';
89  e0 = gamma;
90  x0 = y/norm(y);
91  end
92 end
93 
94 end
95 
96 function f = afun(E,A,B,Z,dt)
97  f = @(x) E*x - dt*A*x + dt*B*(B'*(Z*(Z'*(E*x))));
98 end
99 function f = afun2(E,A,B,Z,dt)
100  f = @(x) E'*x - dt*A'*x + dt*E'*(Z*(Z'*(B*(B'*x))));
101 end
function [ E , A , B , C , x0 ] = riccati_assemble_system_matrices(model, model_data)
model_data)
function gamma = riccati_gamma(model, model_data, sim)
This function calculates gamma. Simdata must be a high dimensional solution.
Definition: riccati_gamma.m:17