Commit 6c764146 authored by Adwait Datar's avatar Adwait Datar
Browse files

Gitlab Runner Update from orc-aux-files/master:Add exercise slides for 7.3,...

Gitlab Runner Update from orc-aux-files/master:Add exercise slides for 7.3, 7.4, 9.1, 9.1*, 9.2 and code for 13.3
parent 3bdc6807
% Image matrix given in Problem 13.3 (Optimal and Robust Control)
% Summer Semester 2020
% Created by Adwait Datar on 26.05.2020
%% Clear Workspace
clear
clc
%% Define Matrix
X = [
38 66 20 47 40 49 55 97 75 69
90 164 54 121 97 126 135 235 186 171
77 141 45 104 83 108 113 201 160 145
77 140 49 103 78 113 112 201 158 142
96 172 61 128 90 146 132 249 189 175
96 176 30 117 153 76 167 243 218 194
55 98 33 72 56 80 79 144 112 103
81 146 56 113 69 135 110 214 160 145
41 77 23 55 50 53 63 107 88 80
83 151 57 116 68 137 111 217 164 149
];
% Plot the Data matrix in the form of color map
plot_matrix_data(X,'X')
%% SVD:
% Compute and plot Singular Values
[n,m] = size(X);
[U,S,V] = svd(double(X));
figure(2); plot(diag(S)), title('Singular values of the image');
plot_SVD_matrices(U,S,V) % Plot the SVD matrices
%% Truncate the matrices for reconstruction of Data
i = 2; % Number of singular values to consider starting from largest
U_tilde = U(:,1:i);
S_tilde = S(1:i,1:i);
V_tilde = V(:,1:i)';
plot_SVD_matrices(U_tilde,S_tilde,V_tilde) % Plot truncated SVD matrices
%% Reconstruct the data matrix and Comparison
X_compressed = U_tilde*S_tilde*V_tilde;
figure()
plot_matrix_data(X_compressed,'X compressed')
X_error=X-X_compressed;
max(max(abs(X_error)))
% Extreme example Rank 1 matrix
% Problem 13.3 Optimal and Robust Control
% Summer Semester 2020
% Created by Adwait Datar on 26.05.2020
%% Clear Workspace
clear
clc
%% Create a random rank one matrix
n=20; % no of columns of Image matrix
m=20; % no of rows of Image matrix
C=256; % range of values for each element of Image matrix
U=sqrt(C)*rand(n,1); % Generate a random column U
V=sqrt(C)*rand(1,m); % Generate a random row V
X=uint8(U*V); % Define the rank 1 matrix X=U*V'
%% Plots
figure()
% Plot Matrix U
subplot(1,3,1)
U_min=min(min(U));
U_max=max(max(U));
range_U = [U_min U_max];
imagesc(U,range_U) % Plots matrix data as a color map
xlim([0,20])
xlim([0.5,20.5])
ylim([0.5,20.5])
title('U')
% Plot Matrix V
subplot(1,3,2)
V_min=min(min(V));
V_max=max(max(V));
range_V = [V_min V_max];
imagesc(V,range_V) % Plots matrix data as a color map
xlim([0.5,20.5])
ylim([0.5,20.5])
title('V')
% Plot Matrix X
subplot(1,3,3)
X_min=min(min(X));
X_max=max(max(X));
range_X = [X_min X_max];
imagesc(X,range_X) % Plots matrix data as a color map
xlim([0.5,20.5])
ylim([0.5,20.5])
title('X')
\ No newline at end of file
% ORC 13.* :: Singular Value Decomposition of a real photo
%
% TUHH :: Institut for Control Systems :: Optimal and Robust Control
% Last update: 26.05.2020
clear
clc
%% Problem - a real photo
RGB = imread('tulips.png');
X = rgb2gray(RGB);
%% Make black & white
d = (0:0.005:1);
map = [d',d',d'];
figure(1); imshow(X,map); title('B&W')
%% Check singular values
[n,m] = size(X);
[U,S,V] = svd(single(X));
figure(2); plot(diag(S(1:200,1:200))), title('Singular values of the image');
%% Try truncating different number of singular values
figure(1);
fprintf('Size in MB = %10.1f Original\n',(n*m)*1*8/1024/1024);
for i=1:10:100
Y = U(:,1:i)*S(1:i,1:i)*V(:,1:i)';
D = X-uint8(Y);
figure(3);
subplot(1,2,1); imshow(Y,map); title('Compressed image')
subplot(1,2,2); imshow(D,map); title('Error')
fprintf('Size in MB = %10.1f\n',(n+m)*i*8/1024/1024);
pause;
end
function []=plot_SVD_matrices(U,S,V)
n=size(U,1);
m=size(V,2);
figure()
subplot(1,3,1)
plot_matrix_data(U,'U')
xlim([0.5,(m+0.5)])
ylim([0.5,(m+0.5)])
subplot(1,3,2)
plot_matrix_data(S,'S')
xlim([0.5,(m+0.5)])
ylim([0.5,(n+0.5)])
subplot(1,3,3)
plot_matrix_data(V,'V^T')
xlim([0.5,(n+0.5)])
ylim([0.5,(n+0.5)])
end
\ No newline at end of file
function []=plot_matrix_data(X_compressed,X_name)
%subplot(1,3,3)
X_minc=min(min(X_compressed));
X_maxc=max(max(X_compressed));
range_Xc = [X_minc X_maxc];
imagesc(X_compressed,range_Xc)
title(X_name)
end
\ No newline at end of file
% ORC 6.2 :: Tune an LQR controller
%
% TUHH :: Institut for Control Systems :: Optimal and Robust Control
% Last update: 15.04.2009
%% System Description
A = [0 1 0;
0 0 1;
0 -2 -3];
b = [0; 0; 1];
c = [1 0 0];
d = 0;
%% Weights
Q = [1 0 0; % 50, 100
0 1 0;
0 0 50]
R = 1 % 0.5, 1
% calculating controller gain
F = -lqr(A,b,Q,R);
%% Non-zero initial conditions
x0 = [1;0;0];
Gi = ss(A+b*F, zeros(3,1), eye(3), 0); % x0 --> x % only the states are of interest
figure(1); initial(Gi, x0, 10);
%% Step response
Gcl = ss(A+b*F, -b*F(1), c, d); % r --> y
Gu = ss(A+b*F, -b*F(1), F, -F(1)); % r --> u
figure(2)
subplot(211), step(Gcl,10); title('y(t)');
subplot(212), step(Gu, 10); title('u(t)');
% ORC 6.3 :: Inifinite Horizon LQR
%
% TUHH :: Institut for Control Systems :: Optimal and Robust Control
% Last update: 24.04.2012
%% System description
A = [0 1; 5 0];
B = [0; 3];
Q = [1 0; 0 0];
R = 1;
%% Hamiltonian and eigenvalues
H = [A -B*inv(R)*B'
-Q -A' ];
[U,L] = eig(H)
%% Solution to the Riccati equation
indices = find(diag(L)<0)
Ustable = U(:,indices);
U11 = Ustable(1:2,:);
U21 = Ustable(3:4,:);
P = U21*inv(U11)
F = -inv(R)*B'*P
% % To check the results
% Flqr = -lqr(A,B,Q,R)
%% Closed-loop system and its eigenvalues
Acl = A+B*F;
eig(Acl)
% ORC 7.4 :: Pendulum (symmetric root-locus)
%
% TUHH :: Institut for Control Systems :: Optimal and Robust Control
% Last update: 07.05.2012
%% System model
A = [ 0 1 0 0;
2.5 0 1 0;
0 0 0 1;
-2.5 0 -2 0];
B = [ 0; 1; 0;-1];
C = [ 1 0 1 0];
D = 0;
G = ss(A,B,C,D);
%% Spectral factorization
[b,a] = tfdata( G, 'v'); % the argument merely returns vectors
% instead of cells
% % In general
% n = length(a);
% b_ = b.*((-1).^[n-1:-1:0]) % construct b(-s)
% a_ = a.*((-1).^[n-1:-1:0]) % a(-s)
% % Now, because there are only even powers
b_ = b;
a_ = a;
bb = conv(b,b_);
aa = conv(a,a_);
GG = tf(bb,aa);
rltool(GG,1e3)
%% Feedback gain
% k = C;
k = 1.5e7;
p = aa + k*bb
rootsp = roots(p)
ind = find(rootsp < 0) % only the stable are of interest
F = - place( A, B, rootsp(ind) )
Gcl = ss( A+B*F, B, C, D);
eig(Gcl)
%% Simulation
x0 = [1;0;1;0];
figure(1), step(Gcl);
figure(2), initial(Gcl, x0)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment