MATLAB Answers

SImple question about Simulink S function

2 views (last 30 days)
Sameer on 18 Jun 2014
Edited: Sameer on 21 Jun 2014
Hello all
How in S function I can obtain:
U(t2) is the velocity at current time step and U(t1) is the velcity at previous time step
Please guide

  1 Comment

Sameer on 18 Jun 2014
Basically objective is to obtain the step size used during the particular iteration. Please guide!!!

Sign in to comment.

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 19 Jun 2014
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.


Sameer on 20 Jun 2014
Thank you very much. I am trying to do but not getting success. Can you please clear it with an example....
Kaustubha Govind
Kaustubha Govind on 20 Jun 2014
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 on 21 Jun 2014
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)
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
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
%%Register methods
block.RegBlockMethod('SetInputPortDimensions', @SetInpPortDims);
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
block.RegBlockMethod('InitializeConditions', @InitConditions);
block.RegBlockMethod('Outputs', @Output);
%block.RegBlockMethod('Update', @Update);
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;
function InitConditions(block)
%%Initialize Dwork
block.Dwork(1).Data = block.DialogPrm(2).Data;
function SetInpPortDims(block, idx, di)
block.InputPort(idx).Dimensions = di;
block.OutputPort(1).Dimensions = di*4;
function Output(block)
[F]=fsolve(@(X) hope(X,block.Dwork(1).Data,TD,10),X);
block.OutputPort(1).Data =F;
%function Update(block)
block.Dwork(2).Data = block.CurrentTime;
where hope is a function returning the 4 equations.
Hope to get further guidance.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by