how to fix this error 'Index exceeds array dimensions. Index value 3 exceeds valid range [1-2] for array 'a'. '

조회 수: 1 (최근 30일)
HI guys, 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 computiong the parameter k for the scalar reference governor. and Hx and Hv is the maximal admissible output sets(MAS) with the matrices A,B,C,D hope can get some help to fix this code form 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
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 5월 4일
That does not look like a MATLAB error message. An Octave error message maybe.
CHEW Yen LI
CHEW Yen LI 2021년 5월 4일
@KSSV HI , the input that I tried is the step input and fyi me is use the matlab function block in the simulink to code this .

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 5월 4일
Replace the loop with
mask = a > 0;
a = a(mask);
b = b(mask);
if isempty(a)
k = 1;
else
k = max(0, min(a./b));
end
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 5월 4일
Okay then, change your code from
k=1;
for i=1:100
if a(i)> 0
k=min(k, b(i)/a(i));
end
end
k=max(k,0);
to
k=1;
for i=1:length(a)
if a(i)> 0
k=min(k, b(i)/a(i));
end
end
k=max(k,0);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by