How to fix looping?
Show older comments
this is my program:
v = 120;
r1 = 1.8;
x1 = 2.4;
r2 = 3.5;
x2 = 1.2;
xm = 60;
ns = 1800;
ws = 188.5;
s = (0.5:1:50)/50;
s (1) = 0.0001;
nm = (1-s)*ns;
for i = 1:51;
zf(i) =(((r2/s(i))+(j*x2))*(j*xm))/(((r2/s(i))+(j*x2))+(j*xm));
zb(i) = (((r2/(2-s(i)))+(j*x2))*(j*xm))/(((r2/(2-s(i)))+(j*x2))+(j*xm));
I(i) = v/(r1+(j*x1)+(0.5*zf(i))+(0.5*zb(i)));
PagF(i) = (abs(I(i)^2))*(0.5*real(zf(i)));
PagB(i) = (abs(I(i)^2))*(0.5*real(zb(i)));
Pag(i)=PagF(i)-PagB(i);
Tind(i) = Pag(i)/ws;
end
figure (1);
plot(nm,Tind,'Color','b','LineWidth',2.0);
grid on;
hold off;
*but after i run this program an error occurred with argument: Attempted to access s(51); index out of bounds because numel(s)=50.
Error in ==> fasbel3 at 15 zf(i) =(((r2/s(i))+(j*x2))*(j*xm))/(((r2/s(i))+(j*x2))+(j*xm));
how to fix it? please help thanks.. with sincerity :)
4 Comments
Craig Cowled
on 15 May 2013
Devia,
Your for _loop has 51 cycles, however, your variable _s _only has 50 elements. There are other problems with your code too. Just change the for loop to _for i = 1:50
There is no need for the semicolon at the end of a _for _statement.
Craig.
Devia Rafika Putri
on 15 May 2013
Craig Cowled
on 15 May 2013
Devia, Andrei's code is much neater. No need for a loop, just use element wise divide './'. And no, I'm not an electrical engineer. I'm a structural engineer working on experimental structural dynamics problems.
Devia Rafika Putri
on 15 May 2013
Accepted Answer
More Answers (1)
Yao Li
on 15 May 2013
0 votes
It seems the length of array s is 50, but you wanna call s(51) in the for loop
4 Comments
Yao Li
on 15 May 2013
for i=1:50
Devia Rafika Putri
on 15 May 2013
Yao Li
on 15 May 2013
Sometimes, I act as an electrical engineer. lol.
Pls. feel free to contact me if u have other questions. Email preferred.
Devia Rafika Putri
on 15 May 2013
Categories
Find more on Assembly in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!