-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from decenter2021/dev
Dev
- Loading branch information
Showing
14 changed files
with
931 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
%% Tutorial of kalmanFiniteHorizonLTV | ||
%% Synthetic random system | ||
T = 50; | ||
n = 5; | ||
o = 3; | ||
rng(1); % Pseudo-random seed for consistency | ||
% Alternatively comment out rng() to generate a random system | ||
% Do not forget to readjust the synthesys parameters of the methods | ||
system = cell(T,4); | ||
% Initial matrices (just to compute the predicted covariance at k = 1) | ||
A0 = rand(n,n)-0.5; | ||
Q0 = rand(n,n)-0.5; | ||
Q0 = Q0*Q0'; | ||
for i = 1:T | ||
if i == 1 | ||
system{i,1} = A0+(1/10)*(rand(n,n)-0.5); | ||
system{i,2} = rand(o,n)-0.5; | ||
else % Generate time-varying dynamics preventing erratic behaviour | ||
system{i,1} = system{i-1,1}+(1/10)*(rand(n,n)-0.5); | ||
system{i,2} = system{i-1,2}+(1/10)*(rand(o,n)-0.5); | ||
end | ||
system{i,3} = rand(n,n)-0.5; | ||
system{i,3} = system{i,3}*system{i,3}'; | ||
system{i,4} = rand(o,o)-0.5; | ||
system{i,4} = system{i,4}*system{i,4}'; | ||
end | ||
E = round(rand(n,o)); | ||
|
||
%% Synthesize regulator gain using the finite-horizon algorithm | ||
% Generate random initial predicted covariance for the initial time instant | ||
P0 = rand(n,n); | ||
P0 = P0*P0'; | ||
% Algorithm paramenters (optional) | ||
opts.verbose = true; | ||
opts.epsl = 1e-5; | ||
opts.maxOLIt = 100; | ||
% Synthesize regulator gain using the finite-horizon method | ||
[K,P] = kalmanFiniteHorizonLTV(system,E,T,P0,opts); | ||
|
||
%% Simulate error dynamics | ||
% Initialise error cell | ||
x = cell(T,1); | ||
% Generate random initial error | ||
x0 = transpose(mvnrnd(zeros(n,1),P0)); | ||
% Init predicted covariance matrix | ||
Ppred = A0*P0*A0'+Q0; | ||
for j = 1:T | ||
% Error dynamics | ||
if j == 1 | ||
x{j,1} = (system{j,1}-K{j,1}*system{j,2})*x0; | ||
else | ||
x{j,1} = (system{j,1}-K{j,1}*system{j,2})*x{j-1,1}; | ||
end | ||
end | ||
|
||
%% Plot the norm of the estimation error | ||
% Plot the ||x||_2 vs instant of the simulation | ||
figure; | ||
hold on; | ||
set(gca,'FontSize',35); | ||
ax = gca; | ||
ax.XGrid = 'on'; | ||
ax.YGrid = 'on'; | ||
xPlot = zeros(T,1); | ||
for j = 1:T | ||
xPlot(j,1) =norm(x{j,1}(:,1)); | ||
end | ||
plot(1:T, xPlot(:,1),'LineWidth',3); | ||
set(gcf, 'Position', [100 100 900 550]); | ||
ylabel('$\|\mathbf{x}_{FH}(k)\|_2$','Interpreter','latex'); | ||
xlabel('$k$','Interpreter','latex'); | ||
hold off; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
%% Tutorial of kalmanOneStepLTV | ||
%% Synthetic random system | ||
T = 50; | ||
n = 5; | ||
o = 3; | ||
rng(1); % Pseudo-random seed for consistency | ||
% Alternatively comment out rng() to generate a random system | ||
% Do not forget to readjust the synthesys parameters of the methods | ||
system = cell(T,4); | ||
% Initial matrices (just to compute the predicted covariance at k = 1) | ||
A0 = rand(n,n)-0.5; | ||
Q0 = rand(n,n)-0.5; | ||
Q0 = Q0*Q0'; | ||
for i = 1:T | ||
if i == 1 | ||
system{i,1} = A0+(1/10)*(rand(n,n)-0.5); | ||
system{i,2} = rand(o,n)-0.5; | ||
else % Generate time-varying dynamics preventing erratic behaviour | ||
system{i,1} = system{i-1,1}+(1/10)*(rand(n,n)-0.5); | ||
system{i,2} = system{i-1,2}+(1/10)*(rand(o,n)-0.5); | ||
end | ||
system{i,3} = rand(n,n)-0.5; | ||
system{i,3} = system{i,3}*system{i,3}'; | ||
system{i,4} = rand(o,o)-0.5; | ||
system{i,4} = system{i,4}*system{i,4}'; | ||
end | ||
E = round(rand(n,o)); | ||
|
||
%% Simulate error dynamics | ||
% Generate random initial predicted covariance for the initial time instant | ||
P0 = rand(n,n); | ||
P0 = P0*P0'; | ||
% Initialise error cell | ||
x = cell(T,1); | ||
% Generate random initial error | ||
x0 = transpose(mvnrnd(zeros(n,1),P0)); | ||
% Init predicted covariance matrix | ||
Ppred = A0*P0*A0'+Q0; | ||
for j = 1:T | ||
% Synthesize regulator gain using the one-step method | ||
[K,Ppred,~] = kalmanOneStepLTV(system(j,:),E,Ppred); | ||
% Error dynamics | ||
if j == 1 | ||
x{j,1} = (system{j,1}-K*system{j,2})*x0; | ||
else | ||
x{j,1} = (system{j,1}-K*system{j,2})*x{j-1,1}; | ||
end | ||
end | ||
|
||
%% Plot the norm of the estimation error | ||
% Plot the ||x||_2 vs instant of the simulation | ||
figure; | ||
hold on; | ||
set(gca,'FontSize',35); | ||
ax = gca; | ||
ax.XGrid = 'on'; | ||
ax.YGrid = 'on'; | ||
xPlot = zeros(T,1); | ||
for j = 1:T | ||
xPlot(j,1) =norm(x{j,1}(:,1)); | ||
end | ||
plot(1:T, xPlot(:,1),'LineWidth',3); | ||
set(gcf, 'Position', [100 100 900 550]); | ||
ylabel('$\|\mathbf{x}_{OS}(k)\|_2$','Interpreter','latex'); | ||
xlabel('$k$','Interpreter','latex'); | ||
hold off; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function [K,Ppred,Pfilt] = kalmanCentralizedLTV(system,Pprev) | ||
%% Description | ||
% This function computes the centralized kalman filter gain for a time-instant | ||
% k, i.e., K(k), subject to a sparsity constraint. | ||
% Input: - system: 1x4 cell whose rows contain matrices A,C,Q and R i.e., | ||
% - system{1,1} = A(k) | ||
% - system{2,2} = C(k) | ||
% - system{3,3} = Q(k) | ||
% - system{4,4} = R(k) | ||
% - Pprev: Previous predicted error covariance matrix, i.e., P(k|k-1) | ||
% Output: - K: filter gain K(k) | ||
% - Ppred: Predicted error covarinace matrix P(k+1|k) | ||
% - Pfilt: Predicted error covarinace matrix P(k|k) | ||
|
||
%% Gain computation | ||
n = size(system{1,1},1); % Get value of n from the size of A | ||
% Compute gain | ||
K = Pprev*transpose(system{1,2})/(system{1,2}*Pprev*transpose(system{1,2})+system{1,4}); | ||
% Update the covariance matrix after the filtering step, i.e., P(k|k) | ||
Pfilt = K*system{1,4}*K'+... | ||
(eye(n)-K*system{1,2})*Pprev*((eye(n)-K*system{1,2})'); | ||
% Compute P(k+1|k) | ||
Ppred = system{1,1}*Pfilt*system{1,1}'+system{1,3}; | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.