Configuring the output of an S-Function

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

采纳的回答

Jesús Zambrano
Jesús Zambrano 2021-7-2
编辑:Jesús Zambrano 2021-7-2
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 个评论
Vagner Martinelli
yes.I changed the initial conditions and it worked.thanks.
Jesús Zambrano
Jesús Zambrano 2021-7-3
编辑:Jesús Zambrano 2021-7-3
Great! Please then mark this as an accepted answer (there is a button named "Accept this Answer") so this question can be closed.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by