Have implemented for loop corectly in my code?

1 visualización (últimos 30 días)
Nisar Ahmed
Nisar Ahmed el 16 de Mayo de 2022
Editada: KALYAN ACHARJYA el 16 de Mayo de 2022
Hi,
I want to put a for loop in my code: can you please confirm if I have done it correctly? Here is my code:
f_min = 1; % Minimum frequency
f_max = 200; % Maximum frequency
f_int = 1; % Interval
%f = f_min:f_int:f_max; % Frequency range
for ii = f_min:f_int:f_max
Logf = log10(f);
w = 2*pi*f;
I1 = sqrt(1i.*w.*s1).*coth(1i.*w.*s1./2);
I2 = sqrt(1i.*w.*s2).*coth(1i.*w.*s2./2);
b = 1./(1+1./(I1.*g1 + I2.*g2));
E = Eo.*b;
Q = real(E)./imag(E);
Qinv = 1./Q;
v=sqrt(E./rho_b);
VP = (real(1./v)).^-1;
end
  2 comentarios
Star Strider
Star Strider el 16 de Mayo de 2022
I do not understand wanting to use a loop when everything already appears to be vectorised. (To use a loop, all the vectors need to be subscripted using the ‘ii’ subscript reference.)
Also this:
VP = (real(1./v)).^-1
does not appear to be appropriate, since it inverts the inversion. Why not just:
VP = real(v);
.
Nisar Ahmed
Nisar Ahmed el 16 de Mayo de 2022
@Star Strider I want to change VP as a function of f and w = 2 pi f, So to compute as a function of w, I need to apply loop.

Iniciar sesión para comentar.

Respuesta aceptada

KALYAN ACHARJYA
KALYAN ACHARJYA el 16 de Mayo de 2022
Editada: KALYAN ACHARJYA el 16 de Mayo de 2022
I don't think, is there any loop needed, it can be avoided, just define the following parameters
s1=..
s2=..
g1=..
g2=..
Eo=..
rho_b=..
More the code:
f_min = 1; % Minimum frequency
f_max = 200; % Maximum frequency
f = f_min:f_max; % Frequency range
Logf= log10(f);
w=2*pi*f;
I1 = sqrt(1i.*w.*s1).*coth(1i.*w.*s1./2);
I2 = sqrt(1i.*w.*s2).*coth(1i.*w.*s2./2);
b = 1./(1+1./(I1.*g1 + I2.*g2));
E = Eo.*b;
Q = real(E)./imag(E);
Qinv=1./Q;
v=sqrt(E./rho_b);
VP=(real(1./v)).^-1;
Here VP is a function of w, you can directly do through vectorized

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by