I am getting an error Index exceeds the number of array elements in the following code. Kindly help me out with this problem.

1 view (last 30 days)
for i=2:n1
V(i)=(2*((v(i))^2)*(v(i+1))^2)-((2*(v(i+1))^2)*((p(i+1)*r(i))+(q(i+1)*x(i))))-(abs(zb)^2)*((p(i+1)^2)+(q(i+1)^2));
Vbase=V(i);
end
for i=2:n1
V(i)=(2*((v(i))^2)*(v(i+1))^2)-((2*(v(i+1))^2)*((P1(i+1)*r(i))+(q(i+1)*x(i))))-(abs(r(i)+x(i))^2)*((P1(i+1)^2)+(q(i+1)^2));
Vload=V(i);
end

Accepted Answer

Cris LaPierre
Cris LaPierre on 20 Sep 2021
We don't have enough information to tell you which variable is causing the problem. However, one of your variables has less than i values, so when you try to index into it using i, your index exceeds the size of the variable, resulting in the error message you are seeing.
% consider a variable with 2 values
a=[1 2];
% Try to index the 3rd value
a(3)
Index exceeds the number of array elements (2).

More Answers (0)

Community Treasure Hunt

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

Start Hunting!