MATLAB Answers

keep getting Index exceeds matrix dimensions.

1 view (last 30 days)
DG
DG on 27 Jul 2021
Edited: DG on 27 Jul 2021
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 Comment
Rik
Rik on 27 Jul 2021
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?

Sign in to comment.

Answers (1)

KSSV
KSSV on 27 Jul 2021
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.

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!