life
life

Reputation: 15

Error in Matlab Function Block: Index exceeds array dimensions

I have some problem during running the code below and it give me the error Index exceeds array dimensions. Index value 3 exceeds valid range [1-2] for array 'a'. Error in 'TEST/TEST' (line 18) if a(i)> 0 This code is the code for computing the parameter k for the scalar reference governor. and Hx and Hv are the maximal admissible output sets(MAS) with the matrices A,B,C,D hope can get some help to fix this code from your all.

function v = SRG_new(v_previous, r)
  A=[0 1;-275.5 -21.22];
  B=[0;1];
  C=[11.02 275.5];
  D=0;
  I=eye(2);
  Hx=(C*A); 
  Hv= C*((I-A)*((I-A)^-1)*B+D); 
  s=350; %s=max_output
  a=Hx*(r-v_previous);
  b=s-Hx-Hv*v_previous;
  k=1;
 
for i=1:100
    if a(i)> 0
        k=min(k, b(i)/a(i));
    end       
end
  k=max(k,0);
  v=v_previous + k*(r-v_previous);
end

Upvotes: 1

Views: 650

Answers (1)

Ben S
Ben S

Reputation: 168

Well this depends mainly on the size your inputs v_previous and r (As The error specifies the range "1-2" i guess, "r" and "v_previous" are scalar values).

The error occurs, because you want to iterate through 100 elements of "a", but as Hx=(C*A); only creates a 1x2 Matrix, a = Hx*(r-v_previous); will result in a Matrix "a" with fewer than 100 entries (Exactly two entries if only multiplied with a scalar value). And this must lead to the "Out of range - Error".

To get rid of the error, simply use

for i=1:numel(a)
    if a(i)> 0
        k=min(k, b(i)/a(i));
    end       
end

This way, it will only iterate through the available array elements.

Upvotes: 1

Related Questions