Integrate in embedded matlab function

1 view (last 30 days)
dab483
dab483 on 26 Nov 2011
Commented: Remus on 17 Jul 2015
Hi,
i am developing continuous EKF using embedded matlab function.In this i need to integrate xhatdot and Pdot. Can i integrate it within embedded matlab function block without post the result and using the integrator outside the embedded block as i need P to calculate K(gain) and xhat to update the states in the block. Any idea how should i solve this? The code as below:
F = [4x4 matrices] %%Jacobian
H = [0 1 0 0; 0 0 1 0]; %%Measurement
xhatdot = [4x4 matices];
%xhatdot(t)--how to integrate to get xhat??? need xhat to update state
Pdot = F * P + F'*P + Q - P * H' * inv(R) * H * P;
Pdot(t)--how to integrate to get P ??? need P to calculate K
%%Kalman gain -K(t)
K =(P*H')/(H*P*H'+R);
%update the state
xhat = xhat + K * (meas - H*xhat);
%%Post the result
xhatout=xhat;

Accepted Answer

Rick Rosson
Rick Rosson on 26 Nov 2011
In order to integrate over time, you need to maintain a state variable (in this case, xhat) across multiple calls of your Embedded MATLAB function. The way to accomplish this outcome is to declare xhat as a persistent variable, and then initialize it the first time the function is called:
persistent xhat
if isempty(xhat)
xhat = 0;
end
...
xhat = xhat + xhatdot*dt;
...
...
HTH.
  6 Comments
Remus
Remus on 17 Jul 2015
Hi all. I have been using the method of outputing the "dx" term and then using as an input to continuous time integrator successfully in the past. However, recently I have been having some difficulties. Here's the issue. I've been using this Level-1 S-function to represent the dynamics of my plant and its been working fine. Because I am trying to transition the model to protected one (where Level 1 sfun are not supported) I converted the s-function to a Simulink function block in series with an integrator (continuous time). The model is running continuous time, ODE45 with Variable step. However, the performance of the overall system has drastically degraded when compared to the Level 1 s-function implementation (in some instances it even goes unstable). Any thoughts on what might cause that?
Remus
Remus on 17 Jul 2015
In case someone else stumbles on the same problem, here is a solution that worked for me. If you are performing the integration method described above, using the continuous time integrator (while in closed loop) doesn't work as good as expected (i.e. system goes unstable) issue the following command at you Matlab prompt: set_param('Name_of_your_Sim_Model','AlgebraicLoopSolver','LineSearch')
It worked for me.

Sign in to comment.

More Answers (1)

Guy Rouleau
Guy Rouleau on 25 Jan 2012
If your EML block is continuous, I recommend outputting the "dx" signal, feed it to an Integrator block and connect the output of the integrator as an Input to the EML block.
This will let the Simulink take care of the job. The solver you choose in the Simulink configuration will be used for the integration (ode45, ode15s, ode3, etc...).

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!