Lab 4 Guide Parameter Estimation
Lab 4 Guide Parameter Estimation
If we have a continuous system: x A x B u
Using zero-order hold on the inputs and a sample time of T seconds, the associated
X( z) b
discrete system is : x(k 1) a x(k ) b u(k ) or equivalently:
U( z) z a
T
where a e AT
b e d B
A
0
Euler approximation: x(kT )
x(k 1) x(k ) x(k 1) 1 A T x(k ) T B u(k )
T
V k v V k b u V(k 1) a V(k ) b u(k ))
A k v a e k v T
T 1 e k v T
k v
B kb b e d k b kb
kv
0
If we had used Euler approximation: a (1 k v T ) b kb T
Master's degree in Automatic Control and Robotics
Problem description
u(k) y(k)
Real
System
ŷ(k ) f (u(k ) , i )
Model
y(k)
practice 4 DATA
ŷ(k ) f (u(k ) , i ) 25
practice 4 DATA
20
18
20
16
14 15
volume
12
volume
10
10
8
4 5
0 0
0 50 100 150 200 250 300 350 400 450 500
0 50 100 150 200 250 300 350 400 450 500
time
Master's degree in Automatic Control and Robotics
N N N
cost function: J e (k)
k 1
2
k 1
( y(k ) y(k ))2 (y(k) f (u(k), ))
k 1
i
2
Data generation
% Practice 4. Parameter estimation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Data generation
rng(5) % Inizialize random seed (for repetitibility)
T=0.2; % Sample time V k v V k b u
kv=0.1;kb=2; % parameters for data generation
A=-kv;B=kb;sys=ss(A,B,1,0); % continuous system sys:dx=A·x+B·u y 1 V
% y=1·x+0·u
% y=x=V=volume (Output = State variable)
% u=v_b (Input variable) A k v B kb
dsys=c2d(sys,T); % it converts to discrete system
[nd,dd]=tfdata(dsys,'v'); % it returns numerator and denominator
% as vectors
b=nd(2);a=-dd(2); % x(k)=a·x(k-1)+b·u(k)
data=[u y];
save data data % Save data for future calculations
Master's degree in Automatic Control and Robotics
Data generation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
plot(y) % Plot the data generated
title('practice 4 DATA')
xlabel('time')
ylabel('volume')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>> data
practice 4 DATA
25 data =
1.0000 0
20 1.0000 0.3274
1.0000 1.0502
1.0000 1.2721
15 1.0000 2.1701
volume
1.0000 2.5198
10
0
0 50 100 150 200 250 300 350 400 450 500
time
Master's degree in Automatic Control and Robotics
Numerical optimization
N
J ( y(k ) c(k )
k 2
T
θ )2 y(N) C(N) θ(N) y(N) C(N) θ(N)
T
min J
xyV
y( 2 ) c (2) y(1)
T u(1)
y(k 1) a y(k ) b u(k )
y ( 3 ) c (3) y(2)
T u(2)
a
y(N) ... C(N) ... ... ... θ(N)
b
The first y estimated is y(2)
...
T
y(N) c (N) y (N 1) u(N 1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Parameter estimation
load data data; % it reads data
u=data(:,1); % it extracts inputs
y=data(:,2); % it extracts outputs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Numerical Optimization
J = @(x)(Y-C*x)'*(Y-C*x); % performance index
theta_est=fminsearch(J,[0;0]); % parameters estimators from numerical
% optimization
%...
Master's degree in Automatic Control and Robotics
Numerical optimization
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Numerical optimization
theta = sdpvar(2,1);
options = sdpsettings('solver',‘quadporg');
optimize(constraints,objective,options); % numerical optimization
theta_est = value(theta);
N
J ( y(k ) c(k )
k 2
T
θ )2 y(N) C(N) θ(N) y(N) C(N) θ(N)
T
dJ
dθ
2 C(N) T y(N) 2 C(N)T C(N) θ(N)
dJ
dθ
1
0 θ(N) C(N) T C(N) C(N) T y(N)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Non recursive method
theta_est_4=(C'*C)\C'*Y; % another way to obtain theta_est
Master's degree in Automatic Control and Robotics
Confidence intervals
θ̂(N) C(N) T C(N) 1
C(N) T y(N)
â
θ̂(N) estimators of parameters a and b
Jθ̂
b̂
s2 estimator of parameter 2
N2
s 2 C(N) T C(N) 1 â
Covariance matrix of estimators θ̂(N)
b̂
â cov z , â cov z Confidence interval for a
11 11
2 2
b̂ cov z , b̂ cov z Confidence interval for b
22 22
2 2
T
exact discret. : a e AT
B
A
b e A d B e AT 1 Euler approx a 1 AT b BT
0
Master's degree in Automatic Control and Robotics
Confidence intervals
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Confidence intervals
sigma2=J(theta_est)/(N-2); % estimation of noise variance
Cov_mat=sigma2*inv(C'*C); % covariance matrix of the parameters estimators
std_error=sqrt(diag(Cov_mat)); % standard errors
Recursive method
ˆ (k 1) ˆ (k) K(k 1) y(k 1) c(k 1) ˆ (k)
Master's degree in Automatic Control and Robotics
Recursive method
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Recursive method
Y=[y(2);y(3)]; %Y(3)
C=[y(1:2) u(1:2)]; %C(3)
P=inv(C'*C); %P(3)
theta_est_rec=P*C'*Y %theta(3)
for i=3:N-1
c=[y(i) u(i)];
D=1+c*P*c'; %Denominator
K=P*c'/D;
theta_est_rec=theta_est_rec+K*(y(i+1)-c*theta_est_rec);
P=P-P*c'*c*P/D;
end
%...
kv_est_rec=-log(theta_est_rec(1))/T; % point estimation of kv
kb_est_rec=kv_est_rec*theta_est_rec(2)/(1-exp(-kv_est_rec*T)); % kb
for i=3:N-1
c=[y(i) u(i)];
D=1+c*P*c'; %Denominator
K=P*c'/D;
theta_est_rec=theta_est_rec+K*(y(i+1)-c*theta_est_rec);
P=P-P*c'*c*P/D;
sigma2=J(theta_est_rec)/(i-2); % estimation of noise variance
Cov_mat=sigma2*P; % covariance matrix
std_error=sqrt(diag(Cov_mat)); % standard errors
figure(2)
plot(kb_est,'r');
hold on;
plot(lbkb);
plot(ubkb);
axis([0 N -1 5]);
title('95% Confidence interval for kb');
xlabel('time');
ylabel('kb estimation');
Master's degree in Automatic Control and Robotics
0.4
4
0.3
0.2
3
0.1
kv estimation
kb estimation
0 2
-0.1
1
-0.2
-0.3
0
-0.4
-0.5 -1
0 50 100 150 200 250 300 350 400 450 500 0 50 100 150 200 250 300 350 400 450 500
time time
Master's degree in Automatic Control and Robotics
Considering state space Then, Kalman filter formulas for parameter estimation are:
form of y(k)=cT (k) 1
L(k) P(k)c(k) R c T (k)P(k)c(k)
A=I
B=0 ˆ (k 1) ˆ (k) L(k) y(k) c T (k)ˆ (k)
C=cT
P(k 1) I L(k)c T (k) P(k)
and no disturbances: Q=0