## How to fix looping?

on 15 May 2013

### Andrei Bobrov (view profile)

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));

Devia Rafika Putri

### Devia Rafika Putri (view profile)

on 15 May 2013

thanks a lot craig.. craig are u an electrical engineer., i hv a more problem..

Craig Cowled

### Craig Cowled (view profile)

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

### Devia Rafika Putri (view profile)

on 15 May 2013

ok craig.. i see.. but some code from andrei, i still dont understand.. but i'll find it..

ooh, I thought you were an electrical engineer

because I'm having problems with programs relating to single phase motor..

thanks so much craig..you are very helpful..

## Products

No products are associated with this question.

### Andrei Bobrov (view profile)

on 15 May 2013
Edited by Andrei Bobrov

### Andrei Bobrov (view profile)

on 15 May 2013

```v = 120;
r1 = 1.8;
x1 = 2.4;
r2 = 3.5;
x2 = 1.2;
xm = 60;
ns = 1800;
ws = 188.5;
```
```s = [.0001;(.5:50).'/50];
s1 = [2-s,s];
z =(r2./s1+1i*x2)*1i*xm./( r2./s1+1i*(x2+xm) );
I = v./(r1+1i*x1+mean(z,2));
Tind = diff(bsxfun(@times,abs(I.^2),real(z)*.5),1,2)/ws;
```
```plot((1-s)*ns,Tind,'Color','b','LineWidth',2.0);
grid on;
```

Devia Rafika Putri

### Devia Rafika Putri (view profile)

on 15 May 2013

your program is work, thanks so much andrei!!! but there is some code that i dont understand..

s = [.0001;(.5:50).'/50];

s1 = [s, 2-s];

Tind = -diff(bsxfun(@times,abs(I.^2),real(z)*.5),1,2)/ws;

what that means? can you explain?? thanks before..

with sincerity Devia

Andrei Bobrov

### Andrei Bobrov (view profile)

on 15 May 2013

Hi Devia!

Rewrite code:

```s = [.0001;(.5:50).'/50];
s1 = [2-s,s];
z =(r2./s1+1i*x2)*1i*xm./( r2./s1+1i*(x2+xm) );
I = v./(r1+1i*x1+mean(z,2));
Tind = diff(bsxfun(@times,abs(I.^2),real(z)*.5),1,2)/ws;
```

as:

```s = [.0001;(.5:50).'/50];
s1 = [2-s,s];
z =(r2./s1+1i*x2)*1i*xm./( r2./s1+1i*(x2+xm) );
I = v./(r1+1i*x1+mean(z,2));
Ia2 = abs(I.^2);
r = real(z)*.5;
P = bsxfun(@times,Ia2,r); % as in your code: P = [PagB PagF];
Pag = diff(P,1,2); % as Pag = PagF - PagB;
Tind = Pag/ws;
```
Devia Rafika Putri

### Devia Rafika Putri (view profile)

on 15 May 2013

Hi Andrei, thank you so much for your explain.. i'm so grateful.

but actually I have another problem. is about 1 phase motors, can you help me?

if you can, I will send my problem to your email..

thanks so much, Andrei..

with sincerity Devia

### Yao Li (view profile)

on 15 May 2013

It seems the length of array s is 50, but you wanna call s(51) in the for loop

Devia Rafika Putri

### Devia Rafika Putri (view profile)

on 15 May 2013

thanks a lot Yao Li.. its work.. are you an electrical engineer? i hv a more problem..

Yao Li

### Yao Li (view profile)

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

### Devia Rafika Putri (view profile)

on 15 May 2013

lol.. thanks so much Yao li, I've sent an email to you .. if you're not busy please check it..

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi