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

1 次查看(过去 30 天)
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 个评论
Endrias
Endrias 2018-6-1
@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 2018-6-1
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).

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-6-1
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by