KalmanFilter
continuous_linear.m
Go to the documentation of this file.
1 clear all;
2 addpath('..');
3 
4 C = [1, 0.1]; %observation matrix to obtain measurement y (y = C*x + noise)
5 A = [-1,0.2;-0.1,-1]; %(d/dt)x(t) = A*x(t)
6 R_c = [0.1^2]; %continuous sensor noise
7 Q_c = [0.001^2,0;0,0.001^2]; %continuous process noise
8 t_start = 0;
9 t_final = 20;
10 outputs = 10000;
11 P_0 = eye(2);%initial covariance
12 
13 %same parameters as for steady_state and continuous_continuous except sensor
14 %matrix is scaled based on time distance between measurements
15 dt = (t_final-t_start)/outputs;
16 R_d = (1/dt).*R_c;
17 
18 %generate data and measurements
19 %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 x_0 = [100; 80]; %initial real data state
21 state_count = length(x_0);
22 sensor_count = 1;
23 ideal_data = zeros(state_count,outputs); %data with no process noise
24 process_noise_data = zeros(state_count,outputs);
25 measurements = zeros(sensor_count,outputs);
26 x = x_0;
27 x_noise = x_0;
28 
29 isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;
30 if isOctave==true
31  randn("seed",1);
32 else
33  rng(1);
34 end
35 
36 for ii=1:outputs
37  x = x + dt.*(A*x);%Explicit Eulers
38  ideal_data(:,ii) = x;%the first column of ideal_data and process_noise_data is x_1
39  x_noise = x_noise + dt.*(A*x_noise) + sqrt(dt.*Q_c)*randn(state_count,1);%Euler-Maruyama
40  process_noise_data(:,ii) = x_noise;
41  measurements(ii) = C*x_noise + sqrt(R_d)*randn(sensor_count,1);
42 end
43 %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44 
45 %set up arguments
46 if isOctave==true
47  f_func = @(x,t) A*x + 0.*t;
48  jacobian_func = @(x,t) A + 0.*x + 0.*t;
49 else
50  f_func = @(x,t) A*x + 0.*t;
51  xx = sym('x',[state_count,1]);
52  jacobian_func = matlabFunction(jacobian(A*xx,xx),'Vars',{xx});
53  jacobian_func = @(x,t) jacobian_func(x) + 0.*t;%no dependence on t
54 end
55 
56 rk4_steps = 4;%type 'help cdekf' for more info
57 [estimates,covariances] = cdekf(f_func,jacobian_func,dt,rk4_steps,t_start,...
58  state_count,sensor_count,outputs,C,chol(Q_c)',chol(R_d)',chol(P_0)',x_0,measurements);
59 
60 covariances{end}
function cdekf(in f_func, in jacobian_func, in dt_between_measurements, in rk4_steps, in start_time, in state_count, in sensor_count, in measurement_count, in C, in Q_root, in R_root, in P_0_root, in x_0, in measurements)