State derivatives returned by S-function 'file.m' in 'file/S-Function' during flag=1 call must be a real vector of length 3
Show older comments
I am trying to simulate the response of my state space system but during the runtime in Simulink I get an error stating "State derivatives returned by S-function 'file.m' in 'file/S-Function' during flag=1 call must be a real vector of length 3". I have narrowed it down to 2 of my state equations which are causing this error as when I commented them out and ran the simulation using only the third equation, the simulation compiled and ran without error. I have searched this error and visited the relevant MathWorks forums but none of them have a solution that works for my case.
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts]=mdlInitializeSizes()
sizes = simsizes;
sizes.NumContStates = 3;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 3;
sizes.NumInputs = 1;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = zeros(3,1);
str = [];
ts = [0 0];
% end mdlInitializeSizes
%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
function sys=mdlDerivatives(t,x,u)
%% Place system here
%change of variables
%states
x_1 = x(1);
x_2 = x(2);
x_3 = x(3);
%inputs
u_1 = u(1);
%system states sys is your state derivatives
sys(1)= x_2^2 + 16*x_2 - 12*u_1 + x_1 - 9*x_3 + x_1*x_3 - 8;
sys(2)= 2*u_1^2 - 7*u_1 + x_1 + 2*x_2*x_3 + 10;
sys(3)= 2*u_1^2 + 8*x_3 + x_1*x_3;
% end mdlDerivatives
%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(t,x,u)
C=eye(3);
sys = C*x;
% end mdlOutputs
The above is a snippet of the code that I am using which is an edited csfunc for a nonlinear system. The equations which are causing the errors are 'sys(1)' and 'sys(2)'.
Any assistance is appreciated.
9 Comments
Walter Roberson
on 2 Mar 2022
Remember that inside Simulink you need to preallocate variables in most cases.
sys = zeros(3,1);
With your current code, when you assign to sys(1) that is the first assignment to the variable and locks it in as having only one element.
Nicholas Bissessar
on 2 Mar 2022
Walter Roberson
on 2 Mar 2022
mdlDerivatives does not preallocate sys
The fact that you initialize x0 in the model initialization function does not mean that you have initialized sys in mdlDerivatives
Nicholas Bissessar
on 2 Mar 2022
Edited: Nicholas Bissessar
on 2 Mar 2022
Walter Roberson
on 2 Mar 2022
One potential risk would be if the input u were purely imaginary. Your third output uses u_1^2 which under that hypothesis would become real-valued. Your other two sys use u_1 not square and those would remain imaginary.
Nicholas Bissessar
on 2 Mar 2022
Walter Roberson
on 2 Mar 2022
Do you know the equilibrium points? Is there a way to calculate them?
I would suggest that you use the simulink debugger to look at the output of the assignment to sys(1) and sys(2) to determine whether the results are complex valued.
Nicholas Bissessar
on 2 Mar 2022
Walter Roberson
on 2 Mar 2022
Right after the line
x0 = zeros(3,1);
assign the appropriate values to x0(1), x0(2), x0(3)
Answers (0)
Categories
Find more on Modeling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!