KalmanFilter
logistic.m
Go to the documentation of this file.
1 clear all;
2 %include code
3 addpath('..')
4 
5 %setting the seed for reproducability
6 isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;
7 if isOctave==true
8  randn("seed",1);
9 else
10  rng(1);
11 end
12 
13 %define discrete logistic growth model and its jacobian
14 rate = 0.01;
15 max_pop = 100;
16 next_func = @(x,t) x + rate.*x.*(1 - x/max_pop) + 0.*t;%time independent
17 jacobian_func = @(x,t) 1 + rate - (2*rate*x)/max_pop + 0.*t;
18 
19 %in this model the time is only necessary
20 %for plotting an interpreting the results.
21 outputs = 1000;
22 t_start = 0;
23 t_final = 10;
24 dt = (t_final - t_start)/outputs;
25 
26 %discrete noise covariance
27 Q_d = 1;
28 %sensor noise covariance
29 R_d = 3;
30 %measurement matrix
31 C = 1;
32 
33 state_count = 1;
34 sensor_count = 1;
35 
36 %model results if there was no process noise
37 ideal_data = zeros(state_count,outputs);
38 %actual data (with process noise)
39 process_noise_data = zeros(state_count,outputs);
40 %noisy measurements of actual data
41 measurements = zeros(sensor_count,outputs);
42 
43 %initial estimate and covariance
44 %the initial estimate in this case
45 %matches the actual data
46 x_0 = max_pop/2;
47 P_0 = 1;
48 
49 %initial condition for ideal
50 %and real process data
51 x = x_0;
52 x_noise = x_0;
53 
54 %generate the data
55 for ii=1:outputs
56  x = next_func(x,ii);
57  ideal_data(:,ii) = x;
58 
59  x_noise = next_func(x_noise,0) + chol(Q_d)'*randn(state_count,1);
60  process_noise_data(:,ii) = x_noise;
61  measurements(ii) = C*x_noise + chol(R_d)'*randn(sensor_count,1);
62 end
63 
64 %filter the noisy measurements
65 [estimates, covariances] = ddekf(next_func,jacobian_func,dt,t_start,state_count,...
66  sensor_count,outputs,C,chol(Q_d)',chol(R_d)',chol(P_0)',x_0, measurements);
67 
68 %compare the measurements, estimates and true state.
69 %The ideal data is not plotted and is only for reference.
70 time = 0:dt:(outputs-1)*dt;
71 h = figure;
72 hold on;
73 plot(time,measurements);
74 plot(time,process_noise_data);
75 plot(time,estimates(1:end-1));
76 legend('Measurement','Real data','Estimate');
77 xlabel('Time')
78 ylabel('Population')
79 
80 h = figure;
81 hold on;
82 limit = 100;
83 plot(time(1:limit),measurements(1:limit),'LineWidth',2);
84 plot(time(1:limit),process_noise_data(1:limit),'LineWidth',2);
85 plot(time(1:limit),estimates(1:limit),'LineWidth',2);
86 legend('Measurement','Real data','Estimate');
87 xlabel('Time')
88 ylabel('Population')
function ddekf(in f_func, in jacobian_func, in dt_between_measurements, 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)