Configuring the output of an S-Function

1 visualizzazione (ultimi 30 giorni)
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:

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programmatic Model Editing in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by