SImple question about Simulink S function

조회 수: 1 (최근 30일)
Sameer
Sameer 2014년 6월 18일
편집: Sameer 2014년 6월 21일
Hello all
How in S function I can obtain:
U(t2)-U(t1)/t2-t1
U(t2) is the velocity at current time step and U(t1) is the velcity at previous time step
Please guide
Regards
  댓글 수: 1
Sameer
Sameer 2014년 6월 18일
Basically objective is to obtain the step size used during the particular iteration. Please guide!!!
Regards

댓글을 달려면 로그인하십시오.

채택된 답변

Kaustubha Govind
Kaustubha Govind 2014년 6월 19일
You can use block.CurrentTime to get the current time in the S-function. You can use a DWork to store the current time at each step and use it in the next step to compute the difference.
  댓글 수: 3
Kaustubha Govind
Kaustubha Govind 2014년 6월 20일
Type msfcndemo_sfundsc2 at your MATLAB prompt to see an example S-function that uses a DWork to store the previous input value. You will need two Dworks, one for the input and one for current-time.
Sameer
Sameer 2014년 6월 21일
편집: Sameer 2014년 6월 21일
Hello....Thank you for letting me know. I tried to write the s function though its running but I guess something is wrong in time,not sure though. Can you have a look and let me know whether I did it in the right manner or not.
function msfcn_equation_trans(block)
setup(block);
%endfunction
function setup(block)
block.NumDialogPrms = 2;
%%Register number of input and output ports
block.NumInputPorts = 1;
block.NumOutputPorts = 1;
%%Setup functional port properties to dynamically
%%inherited.
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
block.InputPort(1).DirectFeedthrough = false;
%block.InputPort(2).DirectFeedthrough = false;
%%Set block sample time to inherited
block.SampleTimes = [-1 0];
%%Set the block simStateCompliance to default (i.e., same as a built-in block)
block.SimStateCompliance = 'DefaultSimState';
%%Run accelerator on TLC
block.SetAccelRunOnTLC(true);
%%Register methods
block.RegBlockMethod('SetInputPortDimensions', @SetInpPortDims);
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
block.RegBlockMethod('InitializeConditions', @InitConditions);
block.RegBlockMethod('Outputs', @Output);
%block.RegBlockMethod('Update', @Update);
%endfunction
function DoPostPropSetup(block)
%%Setup Dwork
block.NumDworks = 2;
block.Dwork(1).Name = 'Intial';
block.Dwork(1).Dimensions = 3;
block.Dwork(1).DatatypeID = 0;
block.Dwork(1).Complexity = 'Real';
block.Dwork(1).UsedAsDiscState = true;
block.Dwork(2).Name = 'time';
block.Dwork(2).Dimensions = 1;
block.Dwork(2).DatatypeID = 0;
block.Dwork(2).Complexity = 'Real';
block.Dwork(2).UsedAsDiscState = true;
%endfunction
function InitConditions(block)
%%Initialize Dwork
block.Dwork(1).Data = block.DialogPrm(2).Data;
block.Dwork(2).Data=block.DialogPrm(1).Data;
%endfunction
function SetInpPortDims(block, idx, di)
block.InputPort(idx).Dimensions = di;
block.OutputPort(1).Dimensions = di*4;
function Output(block)
TD=block.CurrentTime-block.Dwork(2).Data;
X=10*ones(4,1);
[F]=fsolve(@(X) hope(X,block.Dwork(1).Data,TD,10),X);
block.OutputPort(1).Data =F;
%endfunction
%function Update(block)
block.Dwork(1).Data=block.OutputPort(1).Data(2:4);
block.Dwork(2).Data = block.CurrentTime;
%endfunction
where hope is a function returning the 4 equations.
Hope to get further guidance.
Regards

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by