function Info = stab_OP08a_ACC_T2_yal(Ai,b)
% function Info = stab_OP08a_ACC_T2_yal(Ai,b)
% test the robust stability of the a discrete-time time-varying polytopic system with
% bounded parameter variation via affine parameter-dependent Lyapunov function.
% Theorem 2 of (Oliveira & Peres, ACC 2008)
% input:  Ai      -> vertices of the polytope (Ai = [A1 A2 ... A3])
%         b       -> parameters variation bound (0 -> time-invariant, 1 -> arbitrarily fast)
% output: Info.feas     -> feasible (1) or not (0)
%         Info.cpusec   -> cpu time to solve the LMIs (seconds)
%         Info.cpusec_m -> cpu time to mount the LMIs (seconds)
%         Info.Pi       -> Solution variables P
%         Info.K        -> number of scalar variables
%         Info.L        -> number of LMI rows
%
%example: 2 states and 2 vertices
% A1 = [-0.1  1.1;   
%       -0.8  -0.3];  
% A2 = [-0.6  0.6;
%       0.4   -0.3];
% Ai = [A1 A2];
% Info = stab_OP08a_ACC_T2_yal(Ai,0.5);
% 
% Date: 26/06/2008
% Author: ricfow@dt.fee.unicamp.br


%determine the number of vertices of polytope
[order,vertices] = size(Ai);
vertices = vertices / order;

Info.cpusec_m = clock;

%new LMI system
LMIs = set([]);

Info.cpusec_m = clock;

%new LMI system
LMIs = set([]);
Info.L = 0;

for i=1:vertices
    P{i} = sdpvar(order,order,'symmetric');
end

for m=1:vertices
    for i=1:vertices
        Aii = Ai(:,(i-1)*order+1:i*order);


        T = [P{i} Aii'*((1-b)*P{i} + b*P{m});
            ((1-b)*P{i} + b*P{m})*Aii ((1-b)*P{i} + b*P{m})];
        LMIs = LMIs + set(T > 0);
        Info.L = Info.L + 2*order;

        for j=i+1:vertices
            Ajj = Ai(:,(j-1)*order+1:j*order);

            T = [P{i}+P{j} Aii'*((1-b)*P{j} + b*P{m})+Ajj'*((1-b)*P{i} + b*P{m});
                ((1-b)*P{j} + b*P{m})*Aii+((1-b)*P{i} + b*P{m})*Ajj ((1-b)*P{i} + b*P{m})+((1-b)*P{j} + b*P{m})];
            LMIs = LMIs + set(T > 0);
            Info.L = Info.L + 2*order;
        end
    end
end

Info.cpusec_m = etime(clock,Info.cpusec_m);

Info.K = size(getvariables(LMIs),2);

sol = solvesdp(LMIs,[],sdpsettings('verbose',0,'solver','sedumi'));
Info.cpusec = sol.solvertime;
[p,d]=checkset(LMIs);

Info.feas = 0;
%capturing the solutions (if ones exist)
if sum(p < 0) == 0
    Info.Pi = [];
    for i=1:vertices
        Info.Pi = [Info.Pi double(P{i})];
    end
    Info.feas = 1;
end
