Configuring the output of a Matlab S-Function

Asked

Viewed 22 times

0

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 above code, I extract for Simulink the position of the mass through the state x(1) and the derivative of the state position x(2). As shown below:

inserir a descrição da imagem aqui

I want to extract to Simulink through S-Function the system response to the unit step as shown:

inserir a descrição da imagem aqui

Following is the system figure in Simulink:

inserir a descrição da imagem aqui

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.