Continuous vs discrete time

54 views (last 30 days)
VAHA
VAHA on 2 Oct 2023
Commented: VAHA on 2 Nov 2023
Hello,
I am trying to design EKF in MATLAB, where my system dynamics (transition model) has this form --> (1), ​which is continuous in time, but my measurement model is discrete. So in order to convert Eq (1) to a discrete domain, I tried the ODE45 solver, and I expected the result in this form --> (2). But I have been told that the MATLAB ODE45 solver will produce only continuous results, i.e. integrating continuous-time domain differential equations using an ODE45 solver gives results in continuous-time, not in discrete, is that true? So for this reason I used forward Euler (first order RK). Which one is the correct one to transform equation (1) into Eq (2) since EKF in MATLAB expects everything in discrete?
Thanks in advance
Best
Hashir
  2 Comments
Paul
Paul on 2 Oct 2023
Hi Hashir,
The function ode45 does not convert a differential equation into the form of a discrete-time, approximating difference equation. Rather, ode45 solves for an approximate numerical solution to the differential equation, which can be used in an EKF implementation.
Regarding "... since EKF in MATLAB expects everything in discrete?" Does this refer to a specific EKF function that ships with Matlab? If so, can you provided a link to the relevant doc page?
VAHA
VAHA on 3 Oct 2023
Edited: VAHA on 3 Oct 2023
Hi @Paul Paul,
Regarding this statement : "since EKF in MATLAB expects everything in discrete?" what I meant is, in MATLAB the ExtendedKalmanFilter expects the vehicle model and measuement model in discrete-time. Exactly the ode45 approximates the differential equation numerically. So is there any method to convert Eq (1) into (2) completely?

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 2 Oct 2023
It is difficult to follow what you are doing as you describe it. It might be best to use the Control System Toolbox functions if you have access to them, and the extendedKalmanFilter function (introduced in R2016b, improved in R2020b) might be particularly helpful. (I have limited experience with Kalman filters in general, so this reference is likely the best I can do.)
You can force the MATLAB ODE solvers to produce output at specific times by giving it a vector of (ideally a constant sampling interval) times using the linspace function.
  9 Comments
Paul
Paul on 4 Oct 2023
Edited: Paul on 4 Oct 2023
I was following everything, for the most part, until I got to the part about the state transition matrix. The EKF on the linked page, either the continous-discrete or the discrete-discrete, does not use a state transition matrix.
Here's an example that shows using ode45 for the state prediction is, at least, feasible using extendedKalmanFilter for a continuous system with discrete meausrements. However, I'm still not clear on how the extendedKalmanFilter estimates the Jacobian for the state equation. Also, the extendedKalmanFilter is propagating the error covariance for a discrete time system as opposed to integrating the differential equation for the error covariance. Consequently, we have to be careful about the definiton of ProcessNoise, which is a spectral density for continuous time systems and has to be converted to a covariance for discrete-time propagation, if you want ProcessNoise to represent a physical process as opposed to just a user-defined parameter. Anyway, the code below shows how to use ode45 for the state propagation, w/o worrying about any other issues.
Model the truth system and plot the solution
vdpode = @(t,x) [x(2); (1-x(1)^2)*x(2)-x(1)];
x0 = [2;0];
sol = ode45(vdpode,[0 5],x0,odeset('MaxStep',0.05));
figure
plot(sol.x,sol.y)
hold on
Define EKF measurement function
measurementfunction = @(x) x(1);
Define the state prediction function using Euler integration and the EKF with default parameters.
state1 = @(x,dt) x + vdpode(0,x)*dt;
ekf1 = extendedKalmanFilter(state1,measurementfunction,x0);
Execute the EKF. Use the truth data as measurements. Plot the result.
dt = 0.05;
tvec = 0:dt:5;
xhat1 = nan(2,numel(tvec));
for ii = 1:numel(tvec)
meas = deval(sol,tvec(ii),1);
xhat1(:,ii) = correct(ekf1,meas);
predict(ekf1,dt);
end
plot(tvec,xhat1,'o'),grid
Define state prediction function using ode45 and the EKF with default parameters.
% the vdp ode is not dependent on time, so use [0 dt] as the tspan.
state2 = @(x,dt) ode45(vdpode,[0 dt],x).y(:,end);
ekf2 = extendedKalmanFilter(state2,measurementfunction,x0);
Execute and plot
xhat2 = nan(2,numel(tvec));
for ii = 1:numel(tvec)
meas = deval(sol,tvec(ii),1);
xhat2(:,ii) = correct(ekf2,meas);
predict(ekf2,dt);
end
plot(tvec,xhat2,'x'),grid
VAHA
VAHA on 2 Nov 2023
THank you for all your valuable information

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!