Integrate in embedded matlab function
2 次查看(过去 30 天)
显示 更早的评论
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;
0 个评论
采纳的回答
Rick Rosson
2011-11-26
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 个评论
Remus
2015-7-17
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
2015-7-17
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
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...).
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!