matlab coding to s-function level 2 coding.

2 Ansichten (letzte 30 Tage)
dab483
dab483 am 31 Jan. 2012
Hi,
Using Matlab, i can initialize the particle filter like this:
for i = 1 : N
xhatplus(:,i) = xhat + sqrt(P) * [randn; randn; randn];
end
Now, i want to use simulink s-function level 2. how should i coding it?
function DoPostPropSetup(block)
block.Dwork(3).Name = 'xhatplus';
block.Dwork(3).Dimensions = N*3;
block.Dwork(3).DatatypeID = 0;
block.Dwork(3).Complexity = 'Real';
block.Dwork(3).UsedAsDiscState = true;
function InitializeConditions(block)
block.Dwork(3).Data = ??
Any advise? Thanks.
  2 Kommentare
Kaustubha Govind
Kaustubha Govind am 31 Jan. 2012
It depends on what xhat and P stand for in your filter.
dab483
dab483 am 31 Jan. 2012
my coding is something like below:
function DoPostPropSetup(block)
%% Setup Dwork
block.NumDworks = 3;
block.Dwork(1).Name = 'xhat'; %% states
block.Dwork(1).Dimensions = 3;
block.Dwork(1).DatatypeID = 0;
block.Dwork(1).Complexity = 'Real';
block.Dwork(1).UsedAsDiscState = true;
block.Dwork(2).Name = 'P'; %% covariance matrix
block.Dwork(2).Dimensions = 9;
block.Dwork(2).DatatypeID = 0;
block.Dwork(2).Complexity = 'Real';
block.Dwork(2).UsedAsDiscState = true;
N=1000;
block.Dwork(3).Name = 'xhatplus';
block.Dwork(3).Dimensions = N*3;
block.Dwork(3).DatatypeID = 0;
block.Dwork(3).Complexity = 'Real';
block.Dwork(3).UsedAsDiscState = true;
function InitializeConditions(block)
block.Dwork(1).Data = [3e5; -2e4; 1e-3];%%xhat
block.Dwork(2).Data = [1e6;0; 0; 0 ;4e6; 0;0;0;10]; %%P
block.Dwork(3).Data = ??
function Outputs(block)
block.OutputPort(1).Data = block.Dwork(1).Data;%% xhat
%%endfunction
function Update(block)
P=reshape(block.Dwork(2).Data,3,3);
xhatplus=reshape(block.Dwork(3).Data,3,N);

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Kaustubha Govind
Kaustubha Govind am 3 Feb. 2012
Oops, just realized that you are asking about the InitializeConditions function, and not Update or Outputs. The initial condition of your state depends on the dynamics of your system or application - it's hard for us to tell what it should be. The only thing that I can say is that it needs to be of size 3000. For example, for zero-initial condition, you can say:
block.Dwork(3).Data = zeros(3, N);

Weitere Antworten (0)

Kategorien

Mehr zu Fluid Dynamics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by