Configuring the output of an S-Function

2 ビュー (過去 30 日間)
Vagner Martinelli
Vagner Martinelli 2021 年 7 月 1 日
編集済み: Jesús Zambrano 2021 年 7 月 3 日
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
Vagner Martinelli 2021 年 7 月 2 日
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 Center および File ExchangeProgrammatic Model Editing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by