Why do i get "Array indices must be positive integers or logical values." error?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Sandro Lecci
il 1 Giu 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).
Risposta accettata
Walter Roberson
il 1 Giu 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.
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!