MATLAB Answers

Integrate in embedded matlab function

5 views (last 30 days)
dab483 on 26 Nov 2011
Commented: Remus on 17 Jul 2015
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

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;
xhat = xhat + xhatdot*dt;
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...).

Community Treasure Hunt

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

Start Hunting!