Commit 2c5b3461 authored by Lennart Heeren's avatar Lennart Heeren
Browse files

Gitlab Runner Update from atc-aux-files/master:Creates stand alone directories...

Gitlab Runner Update from atc-aux-files/master:Creates stand alone directories for exercise 4.4, 4.5 and 4.6
parent 3aa8682a
function eyeMat = I(n1, n2)
if nargin == 1
eyeMat = eye(n1);
else
eyeMat = eye(n1, n2);
end
\ No newline at end of file
function zeroMat = O(m, n)
if nargin == 1
zeroMat = zeros(m);
else
zeroMat = zeros(m, n);
end
\ No newline at end of file
function [indexMat] = indexMat(n, m)
%%
indexVec = 1:n*m;
indexMat = reshape(indexVec, m, n)';
\ No newline at end of file
function maskedMat = maskMat(mat, maskMat)
%% Normalize to indices from 1, ..., maxIndex
maskMatLin = maskMat(:);
maxIndex = max(maskMatLin);
minIndex = min(maskMatLin(maskMatLin >= 0));
maskMat(maskMat ~= 0) = maskMat(maskMat ~= 0) - (minIndex - 1)*ones(size(maskMat(maskMat ~= 0)));
maxIndex = maxIndex - (minIndex - 1);
minIndex = 1;
%%
maskedMat = mat;
boolMat = maskMat > 0;
maskedMat(boolMat == 0) = 0;
for ii = 1:maxIndex
equalsIndex = find(maskMat == ii);
if ~isempty(equalsIndex)
maskedMat(equalsIndex) = maskedMat(equalsIndex(1));
end
end
function [NumString] = num2strChars(i, chars)
if length(num2str(i)) > chars
error('num2stringChars takes only numbers with less than "chars" characters!');
end
NumString = num2str(i);
numChars = chars - length(NumString);
for curChar = 1:numChars
NumString = ['0', NumString];
end
\ No newline at end of file
function [ParameterListWODuplicates] = removeDuplicateSymVars(ParameterList)
ParameterListWODuplicates = sort(ParameterList);
ii = 1;
while ii < length(ParameterListWODuplicates)
if ParameterListWODuplicates(ii) == ParameterListWODuplicates(ii+1)
ParameterListWODuplicates(ii+1) = [];
else
ii = ii + 1;
end
end
\ No newline at end of file
function save_to_base(overwrite)
%
% save_to_base() copies all variables in the calling function to the base
% workspace. This makes it possible to examine a function's internal
% variables from the Matlab command prompt after the calling function
% terminates.
%
% If the optional argument "overwrite" is present and has a non-zero value,
% variables in the workspace of the caller will overwrite variables in the
% base workspace having the same name. Otherwise, preexisting variables in
% the base workspace will not be overwritten.
%
% Phillip M. Feldman
% Version 1.1, 18 Aug. 2009: Added fix to exclude 'ans' variable.
%
% Version 1.0, 2 June 2009: Initial version.
%
% Note: I'd like to acknowledge assistance from Sean Little of the
% MathWorks.
ws_caller= evalin('caller','who()');
if length(nargin) && overwrite
% Overwrite preexisting variables:
variables= ws_caller;
else
% Do not overwrite preexisting variables:
ws_base= evalin('base','who()');
variables= setdiff(ws_caller, ws_base);
end
for i= 1 : length(variables)
if ~strcmp(variables{i},'ans')
tmp= evalin('caller',variables{i});
assignin('base',variables{i},tmp);
end
end
\ No newline at end of file
function A = symmetrify(A)
A = (A + A')/2;
\ No newline at end of file
syms DP_thth CP_th DP_thp BP_th AP BP_p DP_pth CP_p DP_pp DP_thu BP_u DP_pu DP_yth CP_y DP_yp;
syms DK_thth CK_th DK_thy BK_th AK BK_y DK_uth CK_u DK_uy;
I = 1;
O = 0;
Sigma0 = [ DP_thth, O, CP_th, O, DP_thp ;...
O , O, O , O, O ;...
O , O, I , O, O ;...
O , O, O , I, O ;...
BP_th , O, AP , O, BP_p ;...
O , O, O , O, O ;...
O , O, O , O, I ;...
DP_pth , O, CP_p , O, DP_pp ];
Q = [ O, O, DP_thu ;...
I, O, O ;...
O, O, O ;...
O, O, O ;...
O, O, BP_u ;...
O, I, O ;...
O, O, O ;...
O, O, DP_pu ];
Kth = [ DK_thth , CK_th , DK_thy ;...
BK_th , AK , BK_y ;...
DK_uth , CK_u , DK_uy ];
V = [ O , I, O , O, O ;...
O , O, O , I, O ;...
DP_yth, O, CP_y, O, DP_yp ];
Sigma = Sigma0 + Q*Kth*V;
%% dual
Sigma0d = [ -DP_thth', O, -BP_th', O, -DP_pth' ;...
O , O, O , O, O ;...
-CP_th' , O, -AP' , O, -CP_p' ;...
O , O, O , O, O ;...
O , O, I , O, O ;...
O , O, O , I, O ;...
-DP_thp' , O, -BP_p' , O, -DP_pp' ;...
O , O, O , O, I ];
Qd = [ O, O, DP_thu ;...
I, O, O ;...
O, O, BP_u ;...
O, I, O ;...
O, O, DP_pu ]';
Vd = [ O , I, O , O, O, O, O , O ;...
O , O, O , I, O, O, O , O ;...
DP_yth, O, CP_y, O, O, O, DP_yp, O ]';
Kthd = -[ DK_thth , CK_th , DK_thy ;...
BK_th , AK , BK_y ;...
DK_uth , CK_u , DK_uy ]';
Sigmad = Sigma0d + Vd*Kthd*Qd;
\ No newline at end of file
%% Scherer's Pi
syms Q Q2 Q12;
syms R R2 R12;
syms S S2 S12 S21;
syms Dp Dk;
PiScherer01 = [ Q S Q12 S12;...
S' R S21' R12;...
Q12' S21 Q2 S2 ;...
S12' R12' S2' R2 ];
% This is also what comes out of Scherer06
DeltaScherer01 = [ Dp 0 ;...
1 0 ;...
0 Dk;...
0 1 ];
P1 = [ 0 1 0 0;...
0 0 0 1;...
1 0 0 0;...
0 0 1 0];
P2 = [ 1 0 0 0;...
0 0 1 0;...
0 1 0 0;...
0 0 0 1];
% Wu's Pi
P1*DeltaScherer01
P1*PiScherer01*P1'
P2*DeltaScherer01
P2*PiScherer01*P2'
%%
PiScherer06 = [ R R12 S' S12';...
R12' R2 S21' S2' ;...
S S12 Q Q12 ;...
S21 S2 Q12' Q2 ];
DeltaScherer06 = [ 1 0 ;...
0 1 ;...
Dp 0 ;...
0 Dk];
P2 = [ 1 0 0 0;...
0 0 1 0;...
0 1 0 0;...
0 0 0 1];
% Wu's Pi
P2*DeltaScherer06
P2*PiScherer06*P2'
%%
syms PiRR PiRS PiRQ PiSR PiSS PiSQ
PiRWu06 = [ PiRR PiRS' ;...
PiRS PiRQ ];
PiSWu06 = [ PiSR PiSS' ;...
PiSS PiSQ ];
PiRSc01 = [ PiRQ PiRS ;...
PiRS' PiRR ];
PiSSc01 = [ PiSQ PiSS ;...
PiSS' PiSR ];
PiCLSc06 = [ PiRSc01 PiRSc01-PiSSc01^-1 ;...
PiRSc01-PiSSc01^-1 PiRSc01-PiSSc01^-1 ];
PiCLWu06 = [ PiRWu06 PiRWu06-PiSWu06^-1 ;...
PiRWu06-PiSWu06^-1 PiRWu06-PiSWu06^-1 ];
P1*PiCLSc06*P1'
P2'*PiCLWu06*P2
function [N11,N12,N21,N22] = model_lftNullspace(M11,M12,M21,M22)
[U,S0,V] = svd(M22);
[m,~] = size(M22);
S = S0(:,1:m);
V1 = V(:,1:m);
V2 = V(:,m+1:end);
N11 = M11 - M12*(V1/S)*U'*M21;
N12 = M12*V2;
N21 = (V1/S)*U'*M21;
N22 = V2;
\ No newline at end of file
function PLFR = augment_uss2struct(uPLFR, argnu, argne, uTHETAP);
[ Matrices, ...
Sizes, ...
DeltaBlock] = formulateLMI_plantMatrices(uPLFR , ...
argnu , ...
argne );
[MTHETAP, DELTATHETAP, Blkstruct_THETAP, ~] = lftdata(uTHETAP);
DeltaBlock.THETAP = uTHETAP;
DeltaBlock.rtheta = [Blkstruct_THETAP.Occurrences];
DeltaBlock.ntheta = length(DeltaBlock.rtheta);
DeltaBlock.nTHETA = sum(DeltaBlock.rtheta);
PLFR.Matrices = Matrices;
PLFR.Sizes = Sizes;
PLFR.DeltaBlock = DeltaBlock;
\ No newline at end of file
function [Lj, Deltaj, Rj, subsDeltaNominal] = model_nonDiagonalDeltaFactorization(uDELTA);
[M, Delta, Blkstruct, Normunc] = lftdata(uDELTA);
rDelta = [Blkstruct.Occurrences];
deltaNames = fieldnames(Delta.Uncertainty);
numDeltas = length(deltaNames);
for ii = 1:numDeltas
subsStructNominal.(deltaNames{ii}) = 0;
end
subsDeltaNominal = usubs(uDELTA, subsStructNominal);
for jj = 1:numDeltas
for ii = 1:numDeltas
subsStruct(jj).(deltaNames{ii}) = double(ii == jj);
end
Deltaj{jj} = Delta.Uncertainty.(deltaNames{jj});
subsDelta{jj} = usubs(uDELTA, subsStruct(jj)) - subsDeltaNominal;
[U,S,V] = svd(subsDelta{jj});
Lj{jj} = U*sqrtm(S);
Lj{jj} = removeconstantrows(Lj{jj}')';
Rj{jj} = (sqrtm(S)*V')';
Rj{jj} = removeconstantrows(Rj{jj}')';
end
function PLFR = model_struct2uss(PLFRstruct, varargin)
if (nargin > 1)
PLFRstruct.THETA = varargin{1};
end
% P0 = [PLFRstruct.A , PLFRstruct.B_u ;...
% PLFRstruct.C_y, PLFRstruct.D_yu ];
%
% Q = [PLFRstruct.B_th ;...
% PLFRstruct.D_yth ];
%
% V = [PLFRstruct.C_th, PLFRstruct.D_thu ];
%
%
% nwPth = size(PLFRstruct.THETA, 1);
%
% if nwPth ~= 0
% DeltaBlock = PLFRstruct.THETA;
% S2Delta = eye(nwPth);
%
% Psi = DeltaBlock * (S2Delta - PLFRstruct.D_thth*DeltaBlock)^-1;
%
% PLFRumat = P0 + Q*Psi*V;
% else
% PLFRumat = P0;
% end
%
% nxP = size(PLFRstruct.A, 1);
%
% PLFR = uss(PLFRumat( (1):(nxP), (1):(nxP)), PLFRumat( (1):(nxP), (nxP+1):(end)), ...
% PLFRumat((nxP+1):(end), (1):(nxP)), PLFRumat((nxP+1):(end), (nxP+1):(end)));
%
% [M,Delta,Blkstruct,Normunc] = lftdata(PLFRstruct.THETA);
%
% P0 = [PLFRstruct.D_thth, PLFRstruct.C_th, PLFRstruct.D_thu
% PLFRstruct.B_th , PLFRstruct.A , PLFRstruct.B_u ;...
% PLFRstruct.D_yth , PLFRstruct.C_y , PLFRstruct.D_yu ];
P0 = ss( PLFRstruct.A, [PLFRstruct.B_th, PLFRstruct.B_u], ...
[PLFRstruct.C_th ;...
PLFRstruct.C_y ], ...
[PLFRstruct.D_thth, PLFRstruct.D_thu;...
PLFRstruct.D_yth , PLFRstruct.D_yu ]);
PLFR = lft(PLFRstruct.THETA, P0);
PLFR = simplify(PLFR, 'full');
\ No newline at end of file
function PLFR = model_uss2struct(argPLFR, nu, ny);
[M_PLFR , Theta_PLFR , Blkstruct_PLFR , Normunc_PLFR ] = lftdata(argPLFR);
nxP = size(argPLFR.a , 1);
nzPth = size(Theta_PLFR , 2);
nwPth = size(Theta_PLFR , 1);
nwPp = size(argPLFR.b , 2) - nu;
nzPp = size(argPLFR.c , 1) - ny;
PLFR.A = M_PLFR.a(( 1):(nxP ),( 1):(nxP ));
PLFR.B_th = M_PLFR.b(( 1):(nxP ),( 1):(nwPth ));
PLFR.B_p = M_PLFR.b(( 1):(nxP ),(nwPth +1):(nwPth+nwPp ));
PLFR.B_u = M_PLFR.b(( 1):(nxP ),(nwPth+nwPp+1):(nwPth+nwPp+nu));
PLFR.D_thth = M_PLFR.d(( 1):(nzPth ),( 1):(nwPth ));
PLFR.D_thp = M_PLFR.d(( 1):(nzPth ),(nwPth +1):(nwPth+nwPp ));
PLFR.D_thu = M_PLFR.d(( 1):(nzPth ),(nwPth+nwPp+1):(nwPth+nwPp+nu));
PLFR.D_pth = M_PLFR.d((nzPth +1):(nzPth+nzPp ),( 1):(nwPth ));
PLFR.D_pp = M_PLFR.d((nzPth +1):(nzPth+nzPp ),(nwPth +1):(nwPth+nwPp ));
PLFR.D_pu = M_PLFR.d((nzPth +1):(nzPth+nzPp ),(nwPth+nwPp+1):(nwPth+nwPp+nu));
PLFR.D_yth = M_PLFR.d((nzPth+nzPp+1):(nzPth+nzPp+ny),( 1):(nwPth ));
PLFR.D_yp = M_PLFR.d((nzPth+nzPp+1):(nzPth+nzPp+ny),(nwPth +1):(nwPth+nwPp ));
PLFR.D_yu = M_PLFR.d((nzPth+nzPp+1):(nzPth+nzPp+ny),(nwPth+nwPp+1):(nwPth+nwPp+nu));
PLFR.C_th = M_PLFR.c(( 1):(nzPth ),( 1):(nxP ));
PLFR.C_p = M_PLFR.c((nzPth +1):(nzPth+nzPp ),( 1):(nxP ));
PLFR.C_y = M_PLFR.c((nzPth+nzPp+1):(nzPth+nzPp+ny),( 1):(nxP ));
PLFR.rtheta = [Blkstruct_PLFR.Occurrences];
PLFR.ntheta = size(PLFR.rtheta, 2);
PLFR.THETA = Theta_PLFR;umat2smat(Theta_PLFR);
PLFR.x0 = O(nxP, 1);
PLFR.Ts = argPLFR.Ts;
PLFR.Sizes.nx = nxP;
PLFR.Sizes.nzth = nzPth;
PLFR.Sizes.nwth = nwPth;
PLFR.Sizes.nzp = nzPp;
PLFR.Sizes.nwp = nwPp;
PLFR.Sizes.nu = nu;
PLFR.Sizes.ny = ny;
function dPLFR = uc2d(argPLFR, Ts, method);
[M_PLFR , Theta_PLFR , Blkstruct_PLFR , Normunc_PLFR] = lftdata(argPLFR);
nxP = size(argPLFR.a , 1);
nzPth = size(Theta_PLFR , 2);
nwPth = size(Theta_PLFR , 1);
nwPu = size(argPLFR.b , 2);
nzPy = size(argPLFR.c , 1);
PLFR.A = M_PLFR.a(( 1):(nxP ),( 1):(nxP ));
PLFR.B_th = M_PLFR.b(( 1):(nxP ),( 1):(nwPth ));
PLFR.B_u = M_PLFR.b(( 1):(nxP ),(nwPth +1):(nwPth+nwPu ));
PLFR.D_thth = M_PLFR.d(( 1):(nzPth ),( 1):(nwPth ));
PLFR.D_thu = M_PLFR.d(( 1):(nzPth ),(nwPth +1):(nwPth+nwPu ));
PLFR.D_yth = M_PLFR.d((nzPth +1):(nzPth+nzPy ),( 1):(nwPth ));
PLFR.D_yu = M_PLFR.d((nzPth +1):(nzPth+nzPy ),(nwPth +1):(nwPth+nwPu ));
PLFR.C_th = M_PLFR.c(( 1):(nzPth ),( 1):(nxP ));
PLFR.C_y = M_PLFR.c((nzPth +1):(nzPth+nzPy ),( 1):(nxP ));
switch method
case 'tustin'
% Toth, 2012
Psi = (eye(nxP) - (Ts/2)*PLFR.A)^-1;
dPLFR.A = (eye(nxP) + (Ts/2)*PLFR.A)*Psi;
dPLFR.B_th = sqrt(Ts)*Psi*PLFR.B_th;
dPLFR.B_u = sqrt(Ts)*Psi*PLFR.B_u;
dPLFR.D_thth = (Ts/2)*PLFR.C_th*Psi*PLFR.B_th + PLFR.D_thth;
dPLFR.D_thu = (Ts/2)*PLFR.C_th*Psi*PLFR.B_u + PLFR.D_thu;
dPLFR.D_yth = (Ts/2)*PLFR.C_y *Psi*PLFR.B_th + PLFR.D_yth;
dPLFR.D_yu = (Ts/2)*PLFR.C_y *Psi*PLFR.B_u + PLFR.D_yu;
dPLFR.C_th = sqrt(Ts)*PLFR.C_th*Psi;
dPLFR.C_y = sqrt(Ts)*PLFR.C_y *Psi;
M_dPLFR = ss(dPLFR.A, ...
[dPLFR.B_th, dPLFR.B_u], ...
[dPLFR.C_th; dPLFR.C_y], ...
[dPLFR.D_thth, dPLFR.D_thu;...
dPLFR.D_yth , dPLFR.D_yu ]);
dPLFR = lft(Theta_PLFR, M_dPLFR);
dPLFR.Ts = Ts;
case 'rectangular'
% Toth, 2012
dPLFR.A = (eye(nxP) + (Ts)*PLFR.A);
dPLFR.B_th = (Ts)*PLFR.B_th;
dPLFR.B_u = (Ts)*PLFR.B_u;
dPLFR.D_thth = PLFR.D_thth;
dPLFR.D_thu = PLFR.D_thu;
dPLFR.D_yth = PLFR.D_yth;
dPLFR.D_yu = PLFR.D_yu;
dPLFR.C_th = PLFR.C_th;
dPLFR.C_y = PLFR.C_y;
M_dPLFR = ss(dPLFR.A, ...
[dPLFR.B_th, dPLFR.B_u], ...
[dPLFR.C_th; dPLFR.C_y], ...
[dPLFR.D_thth, dPLFR.D_thu;...
dPLFR.D_yth , dPLFR.D_yu ]);
dPLFR = lft(Theta_PLFR, M_dPLFR);
dPLFR.Ts = Ts;
case 'pade'
% Toth, 2012
Psi = (eye(nxP) - (Ts/2)*PLFR.A)^-1;
dPLFR.A = (eye(nxP) + (Ts/2)*PLFR.A)*Psi;
dPLFR.B_th = [ (Ts/2)*Psi*PLFR.B_th, (Ts/2)*Psi*PLFR.B_th];
dPLFR.B_u = (Ts)*Psi*PLFR.B_u;
dPLFR.D_thth = [ (Ts/2)*PLFR.C_th*Psi*PLFR.B_th + PLFR.D_thth, (Ts/2)*PLFR.C_th*Psi*PLFR.B_th ;...
zeros(size(PLFR.D_thth)) , PLFR.D_thth ];
dPLFR.D_thu = [ (Ts)*PLFR.C_th*Psi*PLFR.B_u + PLFR.D_thu ; ...
PLFR.D_thu ];
dPLFR.D_yth = [ zeros(size(PLFR.D_yth)) , PLFR.D_yth ];
dPLFR.D_yu = PLFR.D_yu;
dPLFR.C_th = [ PLFR.C_th*(eye(nxP) + (Ts/2)*PLFR.A)*Psi ;...
PLFR.C_th ];
dPLFR.C_y = PLFR.C_y;
M_dPLFR = ss(dPLFR.A, ...
[dPLFR.B_th, dPLFR.B_u], ...
[dPLFR.C_th; dPLFR.C_y], ...
[dPLFR.D_thth, dPLFR.D_thu;...
dPLFR.D_yth , dPLFR.D_yu ]);
dPLFR = lft(mdiag(Theta_PLFR, Theta_PLFR), M_dPLFR);
dPLFR = simplify(dPLFR, 'full');
dPLFR.Ts = Ts;
case 'tustinSimple'
% This is most probably quite wrong
[M_PLFR , Theta_PLFR , Blkstruct_PLFR , Normunc_PLFR] = lftdata(argPLFR);
M_dPLFR = c2d(M_PLFR, Ts, method);
dPLFR = lft(Theta_PLFR, M_dPLFR);
dPLFR.Ts = Ts;
otherwise
error('Choose proper method!'); return;
end
\ No newline at end of file
function [uvec, uNvec, ru] = getUncertaintyVector(umat)
[Mumat, U, Blkstruct_umat, Normunc_umat] = lftdata(umat);
ru = [Blkstruct_umat.Occurrences];
unames = fieldnames(U.Uncertainty);