S-function problem

When trying to run my program it says: An error occurred while running the simulation and the simulation was terminated Caused by: State derivatives returned by S-function 'EJEMPLO3' in 'ejemplo3SIMULINK/S-Function' during flag=1 call must be a real vector of length 4 This is the code: function [sys,x0,str,ts] = EJEMPLO3(t,y,u,flag,xo) switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts] = mdlInitializeSizes(xo);
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1,
sys = react(t,y,u,0);
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3,
sys = react(t,y,u,1);
%%%%%%%%%%%%%%%%%%%
% Unhandled flags %
%%%%%%%%%%%%%%%%%%%
case { 2, 4, 9 },
sys = [];
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end % end csfunc
% %============================================================================= % mdlInitializeSizes % Return the sizes, initial conditions, and sample times for the S-function. %=============================================================================
function [sys,x0,str,ts]=mdlInitializeSizes(xo)
sizes = simsizes; sizes.NumContStates = 4; %variables dependientes sizes.NumDiscStates = 0; sizes.NumOutputs = 4; %todas las salidas sizes.NumInputs = 2; %variables independietes o entradas sizes.DirFeedthrough = 1; sizes.NumSampleTimes = 1;
sys = simsizes(sizes); x0 = [xo]; str = []; ts = [0 0];
% end mdlInitializeSizes % %============================================================================= % react % Regresa los estados y par?metros necesarios %=============================================================================
function sys=react(t,y,u,resp) %parametros del sistema v1=150; v2=80; fr=42; k1=9.5537; k2=3.52;
if resp == 0 %MODELO dxdy=[(u(1)*u(2)/v1)+(fr*y(3)/v1)-((u(1)+fr)*y(1)/v1)-(k1*y(1)^2)+(2*k2*y(2));... (fr*y(4)/v1)-((u(1)+fr)*y(2)/v1)+((k1*y(2)^2)/2)-(k2*y(2));... ((u(1)+fr)*y(1)/v2)-((u(1)+fr)*y(3)/v2)-(k1*y(3)^2)+(2*k2*y(4));... ((u(1)+fr)*y(2)/v2)-((u(1)+fr)*y(4)/v2)+((k1*y(3)^2)/2)-(k2*y(4))]; sys = dxdy;
else
%SALIDAS
sys =[y(1) y(2) y(3) y(4)];
end

Réponses (0)

Catégories

En savoir plus sur Modeling dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by