Why do i get "Array indices must be positive integers or logical values." error?

1 view (last 30 days)
I keep getting this error from this function. Does anyone have an idea what is really happening?
%% SIMULATION
function [sys,x0,str,ts] = ctrl_sliding_mode(t,x,u,flag)
switch flag
case 0
[sys,x0,str,ts]=mdlInitializeSizes;
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 = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 3;
sizes.NumInputs = 6;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 0;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [];
function sys = mdlOutputs(t,x,u)
lvp_ref = u(1); %REFERENCE SIGNAL
dlvp_ref = u(2);
lvp = u(3);
beta = u(4);
zeta = u(5);
dlvp = u(6);
eta = 0.2;
c = 10;
e = lvp - lvp_ref;
de = dlvp - dlvp_ref;
s = c*e;
M=2;
if M==1
ut = 1/(c*beta*lvp^zeta)*(-eta*sign(s) + c*dlvp_ref);
elseif M==2 %Saturated function
delta=0.2;
kk = 1/delta;
if abs(s)>delta
sats = sign(s);
else
sats=kk*s;
end
ut = 1/(c*beta*lvp^zeta)*(-eta*sats(s) + c*dlvp_ref);
end
sys(1) = ut;
sys(2)= e;
sys(3) = de;
  7 Comments
Endrias
Endrias on 1 Jun 2018
@Sandro Sats(s) is a function like sign(x) i.e. peicewise. And when i choose e.g. M = 1, i get the next error eventhough i see that a vector of length 3 is passing put as an output...
An error occurred while running the simulation and the simulation was terminated Caused by: Output returned by S-function 'ctrl_sliding_mode' in 'sim_sliding_mode/S-Function' during flag=3 call must be a real vector of length 3;
Sandro Lecci
Sandro Lecci on 1 Jun 2018
What is supposed to do the function sats? According to your if loop when M==2, sats is a variable that stores either the sign of s or the multiplication between s and kk (variables as well).

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 1 Jun 2018
s = c*e but c=10 and e = lvp - lvp_ref but lvp_ref is scalar and lvp = u(3) . So lvp is scalar and so e is scalar and so s is scalar. That would make sign(s) a scalar, and with kk being scalar, kk*s would be a scalar. So sats is a scalar. But you have sats(s) in the ut calculation, which is an indexing request.

More Answers (0)

Categories

Find more on Block and Blockset Authoring in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!