Integrate in embedded matlab function

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;

 采纳的回答

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 个评论

is dt is the integration step size? how to determine that?
Yes. You should set the value of dt to the sampling time of the Embedded MATLAB function block.
Thank you very much. it works. by the way, if i am using a very complicated process model, is that style of integration (xhat=xhat+xhatdot*dt) also can works?
As long as you take your step time small enough, it will work. However, don't make it too small, because that implies doing a huge number of steps, and at each step you will make round-off errors. (and small step time is slower).
You could also implement better integration methods yourself quite easily. The one described above is called 'forward euler'; Runge-Kutta 45 is also quite easy to implement. (you could also try using Matlab's ode45, but I guess that will not work).
Alternatively, you could define xhatdot as an output of your own function, use the standard continuous-time integration block of Simulink and have xhat as an input of your own function again.
Third alternative is to use an S-function (harder, but it will certainly do the job).
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?
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.

请先登录,再进行评论。

更多回答(1 个)

Guy Rouleau
Guy Rouleau 2012-1-25
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...).

类别

帮助中心File Exchange 中查找有关 Parallel Computing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by