keep getting Index exceeds matrix dimensions.

x=[];
ES=[];
kt=140;
kv=140;
L=0.001;
R=10;
b=5;
J=8.6;
kp=240;
ki=180;
A=[0 1 0 0;0 -b/J kt/J 0;-kp/L -kv/L -R/L ki/L;-1 0 0 0];
B=[0;0;kp/L;1];
C=[-1 0 0 0];
D=1;
x0=[30;0;0;0];
sys=ss(A,B,C,D);
for i=1:89
sunTime2(i)=(i-1)*1800;
end
lsim(sys,sunElevation2,sunTime2,x0);
plot(x(1,:));
for i=1:89
ES(i+1)=ES(i)+x(3,i)*(kp*(sunElevation2(i)-x(1,i))+ki*x(4,i));
end;
plot(ES);
The 2nd for loop has an Index that exceeds matrix dimensions.
My purpose is to calculate the Enrgy spent.
I'd appreciate your help with finding the problem.
thanks,
Daniel

댓글 수: 1

lsim(sys,sunElevation2,sunTime2,x0);
Unrecognized function or variable 'sunElevation2'.
Also, what have you tried so far? Did you try using the debugger to determine the size and shape of your variables? Did they match your expectation?

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

답변 (1개)

KSSV
KSSV 2021년 7월 27일

0 개 추천

The error is clear.....you are trying to extract more number of elements than present from an array.
EXample:
A = rand(1,5) ;
A(1) % no error
A(5) % no error
A(6) % error because there are only 5 elements in A and you are extracting 6th element
A(2,1) % error, Rememeber A is a row matrix not a column matrix
Like wise, in you variables from the line:
ES(i+1)=ES(i)+x(3,i)*(kp*(sunElevation2(i)-x(1,i))+ki*x(4,i));
check the dimensions of Es, x, sunElevation. Do they have any dimension of 89? Is x of dimension 4*89?
Did you define Es and initialize it?
The best way to check is learn debugging.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2016a

질문:

DG
2021년 7월 27일

편집:

DG
2021년 7월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by