4 %setting the seed
for reproducability
5 isOctave = exist(
'OCTAVE_VERSION',
'builtin') ~= 0;
12 %define continuous logistic growth model and its jacobian
15 deriv_func = @(x) rate*x*(1 - x/max_pop);%logistic population model (quadratic in x)
17 jacobian_func = @(x) rate - (2*x*rate)/max_pop;%manual derivative - octave might have its own
function 19 %in
case you don
't want to compute the jacobian manually and you have access 20 %to Matlab then you can compute it symbolically and then extract a function from it 22 jacobian_func = matlabFunction(jacobian(deriv_func(xx),xx),'Vars
',xx); 24 deriv_func = @(x,t) deriv_func(x) + 0.*t; 25 jacobian_func = @(x,t) jacobian_func(x) + 0.*t; 28 %in this model the time is only necessary 29 %for plotting an interpreting the results. 33 dt = (t_final - t_start)/outputs; 37 %discrete noise covariance. 39 %continuous process noise covariance 44 %generate data and measurements 48 %model results if there was no process noise 49 ideal_data = zeros(state_count,outputs); 50 %actual data (with process noise) 51 process_noise_data = zeros(state_count,outputs); 52 %noisy measurements of actual data 53 measurements = zeros(sensor_count,outputs); 55 %initial condition for ideal 56 %and real process data 60 %initial condition for ideal 61 %and real process data 69 x = x + dt.*deriv_func(x,curTime); 73 x_noise = x_noise+dt.*deriv_func(x_noise,curTime)+sqrt(dt.*Q_c)*randn(state_count,1); 74 process_noise_data(:,ii) = x_noise; 75 measurements(ii) = C*x_noise + sqrt(R_d)*randn(sensor_count,1); 77 curTime = curTime + dt; 80 %filter the noisy measurements 81 rk4_steps = 1;%internal for code - type 'help
cdekf' for more info 82 [estimates,covariances] = cdekf(deriv_func,jacobian_func,dt,rk4_steps,t_start,... 83 state_count,sensor_count,outputs,C,chol(Q_c)',chol(R_d)
',chol(P_0)',x_0,measurements);
85 %The ideal data is not plotted and is only
for reference
86 time = 0:dt:(outputs-1)*dt;
90 plot(time(1:limit),measurements(1:limit),
'LineWidth',2);
91 plot(time(1:limit),process_noise_data(1:limit),
'LineWidth',2);
92 plot(time(1:limit),estimates(1:limit),
'LineWidth',2);
93 legend(
'Measurement',
'Real data',
'Estimate');
99 plot(0:dt:(outputs-1)*dt,measurements);
100 plot(0:dt:(outputs-1)*dt,process_noise_data);
101 plot(0:dt:(outputs-1)*dt,estimates(1:end-1));
102 legend('Measurement','Real data','Estimate');
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)