What should my input parameter for this Simulink level-2 S-function be?

21 views Asked by At

I used chatgpt to write a code for an s-function block for a system of 2 ODEs. It is supposed to have an input and two outputs. The two ODEs are as follows:

enter image description here

m, g, R, L are constants, T is the input, and the outputs are supposed to be v and theta. The code, saved as myODESFunction.m, is as follows:

function sys = myODESFunction(t, x, u, flag)

m = 384986.21; % Mass
g = 9.81; % Gravity
R = 3.83; % drag constant
L = 59.367; % lift constant

switch flag
    case 0 % Initialization
        sizes = simsizes;
        sizes.NumContStates = 2; % Number of continuous states
        sizes.NumDiscStates = 0; % Number of discrete states
        sizes.NumOutputs = 2; % Number of outputs
        sizes.NumInputs = 1; % Number of inputs
        sizes.DirFeedthrough = 0; % No direct feedthrough
        sizes.NumSampleTimes = 1; % One continuous sample time
        
        sys = simsizes(sizes);

        x0 = [252.222; 0.1745]; % Initial state vector
        str = [];
        ts = [0 0]; % Continuous sample time
        sys = x0;
        sys(2) = str;
        sys(3) = ts;

    case 3 % Update
        sys = [];

    case 1 % Derivatives
        x1 = x(1); % v
        x2 = x(2); % theta
        u1 = u(1); % T

        % Define your differential equations here
        dx1_dt = (-m*g*sin(x2) - R*x1^2 + u1) / m;
        dx2_dt = (-m*g*cos(x2) + L*x1^2) / (m*x1);

        sys = [dx1_dt; dx2_dt];

    case 2 % Outputs
        sys = [x(2); x(1)]; % Output theta and velocity
end

But when I tried to use it in a Level-2 s-function block, by entering [252.222 0.1745] as the two initial conditions in my code into the "s-function parameters" field, it told me I didn't have enough input arguments. But what more could I add?

I tried to remove the input parameter t and flag, but then the error became "switch expression must be a scalar or a character vector"

0

There are 0 answers