Professional Documents
Culture Documents
%% USER INPUT
h = 0.1;
N = 1000;
% model parameters
a = -1;
b = 1;
e = 1;
% initial values for x and u
x = [0 0]';
u = 0;
% initialization of Kalman filter
x_bar = [0 0]';
X_bar = diag([1 1]);
Q = 1;
R = 10;
%% MAIN LOOP
simdata = zeros(N+1,9);
% memory allocation
for i=1:N+1,
t = (i-1)*h;
% time (s)
% measurement
z = x(1)+0.1*randn(1);
% estimation error
z_bar = x_bar(1);
eps
= z-z_bar;
%
u
w
f
Plant
= 0.1*sin(0.1*t);
= 0.1*randn(1);
= [ x(2)
a*x(2)*abs(x(2))+b*u];
E = [0 e]';
x_dot = f + E*w;
% input
% process noise
% dx/dt = f + Ew
=
=
=
=
=
=
simdata(:,1);
simdata(:,2:3);
simdata(:,4:5);
simdata(:,6:7);
simdata(:,8);
simdata(:,9);
clf
figure(gcf)
subplot(221),plot(t,z,'g',t,x_hat(:,1),'r')
xlabel('time (s)'),title('Position x_1'),grid
legend('z=x_1','x_1hat',0)
subplot(222),plot(t,x(:,2),'g',t,x_hat(:,2),'r')
xlabel('time (s)'),title('Velocity x_2'),grid
legend('x_2','x_2hat',0)
subplot(223),plot(t,eps),xlabel('time (s)'),title('Estimation error: z zhat'),grid
subplot(224),plot(t,X_hat),xlabel('time (s)'),title('Diagonal of covariance
matrix X'),grid