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
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
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 Center 및 File Exchange에서 Block and Blockset Authoring에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!