SImple question about Simulink S function

1 次查看(过去 30 天)
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