S-function error

1 visualización (últimos 30 días)
Sun Ning
Sun Ning el 5 de Mayo de 2021
Respondida: Snehal el 20 de Feb. de 2025
hi guys when i try to use the follow s-function in a simulink blocket, i obtain this error:
An error occurred while running the simulation and the simulation was terminated
Caused by:
  • Output returned by S-function 'head_4_2' in 'sim_4_1/S-Function2' during flag=3 call must be a real vector of length 1
function [sys,x0,str,ts] = head_4_2(t,x,u,flag)
switch flag
case 0
[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 = 1;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 1;
sizes.NumInputs = 10;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [0];
str = [];
ts = [0 0];
function sys=mdlDerivatives(t,x,u)
psid=u(1);
dpsid=u(2);
r=u(3);
psi=u(4);
x_1=u(5);
y=u(6);
dr=u(7);
dpsi=u(8);
dx_1=u(9);
dy=u(10);
a0=0.12;a1=0.35;a2=0.5;
k3=2;k4=1.5;k5=50;
zeta=0.3;G=1.525;
alphau=x(1);
dalphau=(dpsid-k4*(psi-psid)-alphau)/zeta;
sys(1)=dalphau;
function sys=mdlOutputs(t,x,u)
alphau=x(1);
psid=u(1);
dpsid=u(2);
r=u(3);
psi=u(4);
x_1=u(5);
y=u(6);
dr=u(7);
dpsi=u(8);
dx_1=u(9);
dy=u(10);
a0=0.12;a1=0.35;a2=0.5;
k3=2;k4=1.5;k5=50;
zeta=0.3;G=1.525;
dalphau=(dpsid-k4*(psi-psid)-alphau)/zeta;
r=dpsi;
z1=r-alphau;
h=-G*(a0+a1*r+a2*r^3);
sigma=(-h+dalphau-k5*z1)/G;
sys(1)=sigma;

Respuestas (1)

Snehal
Snehal el 20 de Feb. de 2025
Hello @Sun Ning,
My understanding is that you are trying to simulate a control system using an ‘S-function’ in MATLAB and Simulink.
Your ‘S-function’ is currently written as a ‘Level-1 S-function’, and the error you're encountering is likely due to the ‘sys’ variable not being assigned correctly as a scalar or a vector of length 1 in the ‘mdlOutputs’ function. This is a common issue with ‘Level-1 S-functions’, which can be less intuitive to debug and manage.
The better approach would be to use ‘Level-2 S-function’ instead of ‘Level-1 S-Function’, because ‘Level-2 S-functions’ offer greater flexibility, ease of maintenance, and higher likelihood for receiving updates and support for new features in Simulink.
You can follow the below steps to implement the logic in ‘Level-2 S-function’:
  1. Transfer your existing logic from the ‘Level-1 S-function’ to the appropriate ‘Level-2’ methods, such as ‘setup’, ‘InitializeConditions’, ‘Outputs’, and ‘Derivatives’
  2. In the ‘setup‘ function, register the number of continuous states, inputs, and outputs
  3. Ensure that block.NumContStates = 1; is specified in the ‘setup’ function to indicate that there is one continuous state. This line is essential for defining the number of continuous states your ‘S-function’ uses, allowing Simulink to allocate the necessary resources and ensure smooth execution of the simulation. Without this specification, any attempt to initialize or access continuous state data will lead to errors
  4. Replace the old ‘S-function’ block in your Simulink model with the new ‘Level-2 S-function’ and ‘run’ the simulation
The error that you were facing earlier should be resolved after this modification.
You can refer to the following documentation links for more details:
I hope this helps!

Categorías

Más información sobre Applications en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by