1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thx for helping me solve the error, the code that you suggest can work thx again for the help
I am glad I was able to help you! Then you can mark the question as solved 👍

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.