MATLAB Answers

Configuring the output of an S-Function

1 view (last 30 days)
I have the following codes in Matlab.
function dx=massamola(t,x,u)
m=1;
k=1;
b=1;
x1 = x(1);
x2 = x(2);
dx1 = x(2);
dx2 = -(k/m)*x1-(b/m)*x2+(1/m)*u;
dx=[dx1;dx2];
soon after I have a specific code of an S-Function.
function [sys,x0,str,ts]=massamola_sfcn(t,x,u,flag,x10,x20)
switch flag
case 0 %initialization
str=[];
ts = [0 0];
s = simsizes;
s.NumContStates=2;
s.NumDiscStates=0;
s.NumOutputs=3;
s.NumInputs=1;
s.DirFeedthrough=0;
s.NumSampleTimes=1;
sys = simsizes(s);
x0 = [x10;x20];
case 1 %derivatives computation
;
sys = massamola(t,x,u);
case 3 %output
sys(1) = x(1); %position
sys(2) = x(2);
case {2 4 9} %2:discrete
%4:calctimeHit
%9:termination
sys = [];
otherwise
error(['unhandled flag=',num2str(flag)]);
end
In case 3 seen in the code above, I extract to Simulink the position of the mass through state x(1) and the derivative of state position x(2). As shown below:
I want to extract to Simulink through the S-Function the system's response to the unit step, as shown in the figure:
Below is a picture of the system system in Simulink:

Accepted Answer

Jesús Zambrano
Jesús Zambrano on 2 Jul 2021
Edited: Jesús Zambrano on 2 Jul 2021
Hi Vagner,
I think it is exactly you already have. Since you put x(1) and x(2) in the S-function output, and these are the unknown described with the two ODEs as described in massamola function, then it is the system response.
The only difference is that you might need to change the initial conditions (values x10 or x20). From the plot, I see it starts from 0 (is it the position or the velocity?).
  4 Comments
Jesús Zambrano
Jesús Zambrano on 3 Jul 2021
Great! Please then mark this as an accepted answer (there is a button named "Accept this Answer") so this question can be closed.

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