0

I am new in Simulink and have a model which is used the S-function. I can not understand how the output of the mdlDerivatives relates to the outputs of the mdlOutputs? In another word, If I want to write a mfile(without using Simulink) what are the states and what are the outputs? I want to write a mfile and find ddq, dq and q but do not get how states in these two parts make the output?(file attached)

function [sys,x0,str,ts]=s_function(t,x,u,flag)
switch flag,
case 0,
e[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 3,
    sys=mdlOutputs(t,x,u);
case {2, 4, 9 }
    sys = [];
otherwise
    error(['Unhandled flag = ',num2str(flag)]);
end

function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates  = 4;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 4;
sizes.NumInputs      = 2;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 0;
sys=simsizes(sizes);
x0=[0;0;0;0];
str=[];
ts=[];
function sys=mdlDerivatives(t,x,u)
q1=x(1);dq1=x(2);
q2=x(3);dq2=x(4);
dq=[dq1;dq2];

% The model is given by Slotine and Weiping Li(MIT 1987)
alfa=6.7;beta=3.4;
epc=3.0;eta=0;
m1=1;l1=1;
lc1=1/2;I1=1/12;
g=9.8;
e1=m1*l1*lc1-I1-m1*l1^2;
e2=g/l1;

H=[alfa+2*epc*cos(q2)+2*eta*sin(q2),beta+epc*cos(q2)+eta*sin(q2);
   beta+epc*cos(q2)+eta*sin(q2),beta];
C=[(-2*epc*sin(q2)+2*eta*cos(q2))*dq2,(-epc*sin(q2)+eta*cos(q2))*dq2;
   (epc*sin(q2)-eta*cos(q2))*dq1,0];
G=[epc*e2*cos(q1+q2)+eta*e2*sin(q1+q2)+(alfa-beta+e1)*e2*cos(q1);
   epc*e2*cos(q1+q2)+eta*e2*sin(q1+q2)];

tol(1)=u(1);
tol(2)=u(2);

ddq=inv(H)*(tol'-C*dq-G);
sys(1)=x(2);
sys(2)=ddq(1);
sys(3)=x(4);
sys(4)=ddq(2);
function sys=mdlOutputs(t,x,u)
sys(1)=x(1);
sys(2)=x(2);
sys(3)=x(3);
sys(4)=x(4);
mvtn
  • 35
  • 8
  • The output of this block coincides with its state. `mdlOutputs` receives the second argument `x` (this `x` is the state of the system) and returns it in `sys` – AVK Nov 13 '17 at 11:52

1 Answers1

0

A non-linear state-space model is comprised of 2 equations:

x_dot = f(x,u,t)

y = g(x,u,t)

If the system is linear this reduces to the (perhaps more familiar form)

x_dot = A * x + B * u

y = C * x + D * u

For a level-1 m-code S-Function (which is what you have), when Simulink wants the block's state derivative to be calculated it calls mdlDerivatives, and when it wan't the block's outputs it calls mdlOutputs.

That is mdlDerivatives implements the above equation for x_dot, and mdlOutputs implements the above equation for y.

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28