Skip to content

Instantly share code, notes, and snippets.

@yaroslavvb
Created June 30, 2022 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaroslavvb/a537cf46e75016da31c995b33cad58ca to your computer and use it in GitHub Desktop.
Save yaroslavvb/a537cf46e75016da31c995b33cad58ca to your computer and use it in GitHub Desktop.
Matlab's Lyapunov solver code
function X = lyap2(A, B, C)
%LYAP2 Lyapunov equation solution using eigenvalue decomposition.
% X = LYAP2(A,C) solves the special form of the Lyapunov matrix
% equation:
%
% A*X + X*A' = -C
%
% X = LYAP2(A,B,C) solves the general form of the Lyapunov matrix
% equation:
%
% A*X + X*B = -C
%
% LYAP2 is faster and generally more accurate than LYAP except when
% A or B have repeated roots.
%
% See also DLYAP.
% A.C.W. Grace 10-25-89
% Copyright 1986-2002 The MathWorks, Inc.
[ma,na] = size(A);
[mb,nb] = size(B);
if ((ma ~= na) | (mb ~= nb))
error('Dimensions of A and B do not agree.');
elseif ma==0,
X = [];
return
end
% Perform eigenvalue decomposition on A and B
[T,DA]=eig(A);
if nargin==3
[mc,nc] = size(C);
if ((mc ~= ma) | (nc ~= mb)), error('Dimensions of C do not agree'); end
[U,DB]=eig(B);
X=-T*(T\C*U./(DA*ones(ma,mb)+ones(ma,mb)*DB))/U;
else
% Shortcut for AX+XA'+C=0
% Note B is really C for two input arguments
IT=inv(T);
DA=DA*ones(ma,ma);
X=-T*(IT*B*IT.'./(DA+DA.'))*T.';
C=0;
end
% ignore complex part if real inputs (better be small)
if ~(any(any(imag(A))) | any(any(imag(B))) | any(any(imag(C))))
X = real(X);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment