Lab 4 Guide State Estimation
Lab 4 Guide State Estimation
20
data = 18
1.0000 10.0000 1.5269
volume
N N
min J w(k )
k 1
T
Q 1
w(k ) v(k )
k 1
T
R 1 v(k )
1
w(1)T P1 w(1)
N
min J x̂(k 1) A x̂(k) B u(k)
k 1
T
Q 1 x̂(k 1) A x̂(k) B u(k)
N
y(k) C x̂(k) D u(k)
k 1
T
R 1 y(k) C x̂(k) D u(k)
x 1 x̂(1)T P1 1 x 1 x̂(1)
Master's degree in Automatic Control and Robotics
Model discretization
Given u(1)…u(N) and y(1), …y(N) determining the optimal estimate x̂(N 1)
a e k v T
V k v V k b v b V(k 1) a V(k ) b v b (k )
1 e k v T
qout k v V q out (k ) k v V (k ) b kb
kv
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Problem formulation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% solution of the problem
options = sdpsettings('solver',‘quadprog');
optimize(constraints,objective,options);
x_est = zeros(N+1,1);
for k = 1:N+1
x_est(k,1) = value(x_hat{k});
end
Master's degree in Automatic Control and Robotics
Optimization formulation of the prediction problem
true state (blue) ; estimation (red) ; output/kv(green)
25
20
volume
15
10
0
0 20 40 60 80 100 120
time
Master's degree in Automatic Control and Robotics
Lab 4: Optimal Estimation
Master's degree in Automatic Control and Robotics
Kalman filter
P(1)
x̂(1)
L(k ) A P(k ) C T R C P(k ) C T 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Kalman filter with confidence interval
x=zeros(N+1,1); % initialization of states
P=zeros(N+1,1); % initialization of P matrix
L=zeros(N,1); % initialization of L matrix
lb=zeros(N+1,1); % lower bounds of states
ub=zeros(N+1,1); % upper bounds of states
alpha= % select according confidence interval
x(1)=x1; % initial condition for x;
P(1)=P1; % initial condition for P;
lb(1)=x(1)-sqrt(P(1))*1.96;
ub(1)=x(1)+sqrt(P(1))*1.96;
figure(2)
plot(x,'r'); % plot state estimation
hold on;
plot(lb,'b'); % plot lower bounds interval
plot(ub,'b'); % plot upper bound interval
Master's degree in Automatic Control and Robotics
Lab 4: Optimal Estimation
Master's degree in Automatic Control and Robotics
L(k ) A P(k ) C R C P(k ) C
T
T 1
ans =
0.1774 0.2000
0.3568 0.4105
T 1
0.3671 0.4228
L A P C R C P C
T 0.3748 0.4321
0.3806 0.4391
0.3850 0.4444
P Q A L C P A T
0.3883 0.4484
0.3908 0.4513
0.3926 0.4536
0.3940 0.4552
0.3950 0.4565
0.3958 0.4574
P Q A A P CT R C P CT C P A T
0.3964 0.4581
1 0.3968 0.4587
0.3971 0.4590
0.3974
0.3976
0.4593
0.4596
0.3977 0.4597
syms z 0.3978 0.4598
0.3979 0.4599
z=vpa(solve(z==Q+(a-a*z*c*(R+c*z*c)^-1*c)*z*a,z,'Real',true)) 0.3979 0.4600
PSS=z(1); 0.3980 0.4601
0.3980 0.4601
LSS=a*PSS*c*(R+c*PSS*c)^-1; 0.3980 0.4601
0.3980 0.4601
0.3981 0.4602
0.3981 0.4602
0.3981 0.4602
0.3981 0.4602
0.3981 0.4602
0.3981 0.4602
0.3981 0.4602
0.3981 0.4602
0.3981 0.4602
…
Master's degree in Automatic Control and Robotics
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Kalman filter with confidence interval using steady state
x=zeros(N+1,1); % initialization of states
lb=zeros(N+1,1); % lower bounds of states
ub=zeros(N+1,1); % upper bounds of states
alpha= % choose the desired confidence
x(1)=x1; % initial condition for x;
lb(1)=x(1)-alpha*sqrt(PSS);
ub(1)=x(1)+alpha*sqrt(PSS);
figure
plot(x,'r'); % plot state estimation
hold on;
plot(lb,'b'); % plot lower bounds interval
plot(ub,'b'); % plot upper bound interval
Master's degree in Automatic Control and Robotics
20
18
16
14
12
10
8
0 20 40 60 80 100 120
Master's degree in Automatic Control and Robotics
20 20
18 18
16 16
14 14
12 12
10 10
8 8
0 20 40 60 80 100 120 0 20 40 60 80 100 120
Master's degree in Automatic Control and Robotics
e
x1_e 0 0 2
To: y1
1.5
𝑢(𝑘) 𝑦(𝑘)
Amplitude
0.5
𝑥(𝑘) 25
20
𝑢(𝑘)
e
15
𝑦(𝑘)
To: x1
𝑦(𝑘) 𝑥 (𝑘)
10
𝑥 (𝑘) 5
0
10 20 30 40 50 60 70 80 90 100
Time (seconds)
Master's degree in Automatic Control and Robotics
The Ricatti equation for the LQR problem can be formulated as the following LMI:
Y YAT W T BT YH T WT where:
AY BW Y 0 0 Q HTH
0
HY 0 I 0 Y P 1
W 0 0 R 1 K WY 1
The optimal value of K can be found considering that J opt xo Pxo by solving
T
min I I
where:
I Y 0
W ,Y
xoT Pxo
where:
Y YAT W T BT YH T WT Y P 1
AY BW Y 0 0
0
HY I I
0 I 0 I Y 0
W 0 0 R 1
Master's degree in Automatic Control and Robotics
Steady state Kalman filter using LMIs
T=1; % sample time
kv=0.1; % parameter kv
kb=2; % parameter kb
a=exp(-kv*T); % parameter a (using exact discretization)
b=(1-exp(-kv*T))*kb/kv; % parameter b (using exact discretization)
c=kv; % parameter c
d=0; % parameter d
Q=0.1; % deviation of system noise (disturbance)
R=0.1; % deviation of measurement noise
%LMI solution
A=a'; % Duality principle for observer design
B=c'; % Duality principle for observer design
Y = sdpvar(1,1);
W = sdpvar(1,1);
gamma=sdpvar(1,1);
H=sqrt(Q);
F = [Y >= 0];
F = [F, [gamma*eye(1) eye(1);eye(1) Y]>=0];
% LQR LMI
optimize(F,gamma)
x_0=sdpvar(1,1);
yd=sdpvar(N,1);
ud=sdpvar(N,1);
objective = (x_0-x_hat{1})'*P_0^(-1)*(x_0-x_hat{1});
constraints = [];
for k = 1:N
objective = objective+...
(x_hat{k+1}-A*x_hat{k}-B*ud(k))'*Q^(-1)*(x_hat{k+1}-A*x_hat{k}-B*ud(k))...
+(yd(k)-C*x_hat{k})'*R^(-1)*(yd(k)-C*x_hat{k});
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% solution of the problem
options = sdpsettings('solver',‘quadprog');
estimator=optimizer(constraints,objective,options, {yd,ud,x_0},x_hat{N+1});
Master's degree in Automatic Control and Robotics
MHE via numerical optimization
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% implement MHE via numerical optimization
M=100;
x_est=zeros(M+1,1); % initialization of states
%initialize the first N state estimation using the result of exercise (a)
...
for k=N+1:M+1
x_est(k+1)=estimator(y(k-N:k-1),u(k-N:k-1),x_est(k-N));
end
25
20
volume
15
10
0
0 20 40 60 80 100 120
time