Search code examples
matlabmath

How to force matlab output any solution when the Lyapunov solution is not unique?


I want to solve the Lyapunov equation of the following form

and I use this code in Matlab: Lambda = lyap(Cbar,Cbar,-2*Cbarprime); However, the Matlab displays the error message: The solution of this Sylvester equation does not exist or is not unique. How can I force it to output any one solution?


Solution

  • We can use the following code which is amended from the official function sylvester by changing some of the elements(actually zero elements) in matrix dA+dB' to be inf.

    function X = mysylvester(A,B,C)
    %         Solve the equation A*X + X*B = C 
    A = (A + A') / 2;
    B = (B + B') / 2;
    [QA, dA] = eig(A, 'vector');
    [QB, dB] = eig(B, 'vector');
    
    CC = QA'*C*QB;
    tmp = dA + dB';
    idx = find(abs(tmp) <= 0.00001);
    tmp(idx) = inf;
    X = CC ./ tmp;
    X = QA*X*QB';
    end