4 views (last 30 days)

Hi, I am having to learn and apply for loops in my new job.

I have an equation which contains two variables each time, i.e. I have to compute at three different frequencies and at each of these frequencies there is a separate current. I have attached the code:

% Parameters

a_R = 0.0325;

a_T = 0.0325;

N_T = 164;

N_R = 10;

mu_0 = 4*pi*(10^-7);

f_Array = [10000,20000,40000];

w = 2*pi*f_Array; % Angular frequency

I = [2,1,0.5]; % Current

for i = 1:3

f = f_Array(i);

switch f

case 10000

I = 2;

case 20000

I = 1;

case 40000

I = 0.5;

end

K_R = ((mu_0^2)*(w(i)^2)*pi*N_T*N_R*(a_T^2)*(a_R^2)*I./(4*x_R_center));

end

Basically, the output I want is K_R at each frequency and respective curent, however the output just runs the first case and not the other two.

Any help would be greatly appreciated. Thanks.

Andrei Bobrov
on 17 Oct 2019

Edited: Andrei Bobrov
on 17 Oct 2019

% Parameters

a_R = 0.0325;

a_T = 0.0325;

N_T = 164;

N_R = 10;

mu_0 = 4*pi*(10^-7);

f_Array = [10000,20000,40000];

w = 2*pi*f_Array; % Angular frequency

I = [2,1,0.5]'; % Current

K_R = mu_0^2*w.^2*pi*N_T*N_R*a_T^2*a_R^2.*I/(4*x_R_center);

If you use old MATLAB (R < 2016a), so last expresion:

K_R = mu_0^2*pi*N_T*N_R*a_T^2*a_R^2/(4*x_R_center)*bsxfun(@times,w.^2,I);

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485868-how-to-include-a-switch-statement-within-a-for-loop#comment_757294

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485868-how-to-include-a-switch-statement-within-a-for-loop#comment_757294

Sign in to comment.