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

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 Kommentare

Can you provide the error message with all the line numbers?
and what are t, x, u?
Hi Endrias,
Which line gives you this error?
I'm tring to simulate a controller in simulink and this is what i got from Diagonstic Viewer window.
Simulation 1 Clear
08:40 AM Elapsed: 0.653 sec
An error occurred while running the simulation and the simulation was terminated
Caused by:
Error in 'sim_sliding_mode/S-Function' while executing MATLAB S-function 'ctrl_sliding_mode', flag = 3 (output), at time 0.0.
Array indices must be positive integers or logical values.
I think the problem is here:
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
is sats a variable? if yes then why you want to access a specific coordinate (s)?
@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;
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).

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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.

1 Kommentar

You are prefectly right @Walter. It works now ... thank you very much all!!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by