Index exceeds the number of array elements

clc
clear
r=0;x=9;
wg=40e6; vo=30e3;voa=0;
for j=1:121
pf(j)=0.4+0.01*(j-1);
if pf<=1
cons(j)=acos(pf(j));
elseif pf>1
cons(j)=-acos(2-pf(j));
end
ia(j)=(wg./(sqrt(3).*vo.*abs(pf(j)))).*(cos(acos(pf(j)))+sin(cons(j)).*i);
eph(j)=(vo./(sqrt(3))).*(cosd(voa)+sind(voa).*i)+ia(j)*(r+i*x);
E(j)=sqrt(3).*abs(eph(j));
Ifex(j)=abs(E(j))./3464.1;
iax(j)=abs(ia(j));
end
Index exceeds the number of array elements. Index must not exceed 61.
plot(Ifex,iax)
grid
xlabel('Field current (A)')
ylabel('Armature current (A)')
title('V curve of synch. generator')
if i juse else only in the if statement, the code will run but the plot will be incomplete, and if i put elseif pf>1, I get an error

1 Comment

I am trying to make a colon that starts from 0.4 to 1 and then from 1 to 0.4 , which i used pf(j)=0.4+0.01*(j-1); if there is another method that I can do it, it might help me significantly plot such function.

Sign in to comment.

 Accepted Answer

In your if and elseif conditions you are comparing the vector pf with the scalar 1. You should use pf(j) instead of pf in those places, in order to compare the jth element of pf with 1.
(And, as you point out, the elseif can be replaced with just else. After you change pf to pf(j), the else block will be executed for some j, and cons will have the same size as pf, avoiding this error.)

More Answers (0)

Products

Release

R2021a

Asked:

on 7 Dec 2021

Answered:

on 7 Dec 2021

Community Treasure Hunt

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

Start Hunting!