How can I combine if statement inside for loop?

clc
clear all
t = [-0.095; -3.953; -7.52; 7.59; 37.66]
m6 = 1.4544e+06;
m5 = -5.7624e+05;
m4 = -1.8298e+08;
m3 = -6.3483e+08;
m2 = -1.0784e+08;
m1 = -8.3759e+07;
m0 = -7.4807e+06;
g5 = -1.8497e+06;
g4 = -7.9658e+06;
g3 = -1.5743e+07;
g2 = -3.0234e+06;
g1 = -2.0215e+06;
g0 = -1.7686e+05;
R22 = 0.4752;
for i=1:length(t)
ti=t(i)
X(i) = m0 + m1*ti + m2*ti^2 + m3*ti^3 + m4*ti^4 + m5*ti^5 + m6*ti^6;
Y(i) = g0 + g1*ti + g2*ti^2 + g3*ti^3 + g4*ti^4 + g5*ti^5;
Z(i) = X(i)/Y(i);
if Z(i) > 0
wc(i) = sqrt(R22/Z(i))
td(i) = (2/wc(i))*atan(wc(i)*ti);
else
return
end
td = min(td(j))
end
The for loop is intended to compute values of Z(i) as per corresponding values of t (i). Then, if the value of Z(i) is positive value, only then the values of wc(i) and td(i) going to be computed. For all possible values of td(i), the chosen value going to be the minimum of all.

 Respuesta aceptada

DGM
DGM el 3 de Jun. de 2021
Editada: DGM el 3 de Jun. de 2021
I don't know what this is trying to do, so here goes.
t = [-0.095; -3.953; -7.52; 7.59; 37.66];
m6 = 1.4544e+06;
m5 = -5.7624e+05;
m4 = -1.8298e+08;
m3 = -6.3483e+08;
m2 = -1.0784e+08;
m1 = -8.3759e+07;
m0 = -7.4807e+06;
g5 = -1.8497e+06;
g4 = -7.9658e+06;
g3 = -1.5743e+07;
g2 = -3.0234e+06;
g1 = -2.0215e+06;
g0 = -1.7686e+05;
R22 = 0.4752;
% preallocate, otherwise vectors will have mismatched length
% and indexing errors will happen
N = numel(t);
X = zeros(N,1);
Y = zeros(N,1);
Z = zeros(N,1);
wc = NaN(N,1);
td= NaN(N,1);
for i=1:N
ti=t(i);
X(i) = m0 + m1*ti + m2*ti^2 + m3*ti^3 + m4*ti^4 + m5*ti^5 + m6*ti^6;
Y(i) = g0 + g1*ti + g2*ti^2 + g3*ti^3 + g4*ti^4 + g5*ti^5;
Z(i) = X(i)/Y(i);
if Z(i) > 0
wc(i) = sqrt(R22/Z(i));
td(i) = (2/wc(i))*atan(wc(i)*ti);
% return halts all execution -- everything inside and outside the loop
% if you want to break out of the loop conditionally, use break
% if you want to skip to the next loop cycle, use continue
end
end
tdmin = min(td(~isnan(td))) % this doesn't make any sense to be inside the loop
tdmin = -0.1900
As an aside,
% by default, both i and j are sqrt(-1).
% that's part of why using them as indices can lead to problems
td = min(td(j)) % j was not user-defined as an index
Array indices must be positive integers or logical values.

5 comentarios

Suud Hasen
Suud Hasen el 3 de Jun. de 2021
Editada: Suud Hasen el 3 de Jun. de 2021
The intention in the if statement is to yield angular frequency for positive Z(i) only. Z(i) has possible 5 values of 42.8435, -933.58, -1.7141, 7.8419 and -23.5903. Only for the positive values, computes wc(i) and td(i), then, take td the minimum.
DGM
DGM el 3 de Jun. de 2021
Editada: DGM el 3 de Jun. de 2021
I didn't assume that the input values were the only possible ones. In this case, min(td)<0, but if min(td)>0, then there would be a problem. As the comment mentions, using zeros() to create the empty array means the default zeros in the positions associated with Z>0 would interfere with correctly calculating min(td). If instead you used NaN(), the default values would be NaN and could unambiguously be distinguished from valid td values and excluded from the minimization.
EDIT:
I went ahead and edited the code to use NaN() instead of zeros() and excluded invalid entries from the minimization. You can do something similar with wc if you want.
Suud Hasen
Suud Hasen el 3 de Jun. de 2021
Editada: Suud Hasen el 3 de Jun. de 2021
Yes, you are right. Value must be td > 0. Delay margin value in Matlab/Simulink shows td_min supposed to be 8.7695. Which is one of the 2 possible results.
Weeell for simplification, I just rewrote this without the loop. It's easier to see what's going on.
t = [-0.095; -3.953; -7.52; 7.59; 37.66]
t = 5×1
-0.0950 -3.9530 -7.5200 7.5900 37.6600
m6 = 1.4544e+06;
m5 = -5.7624e+05;
m4 = -1.8298e+08;
m3 = -6.3483e+08;
m2 = -1.0784e+08;
m1 = -8.3759e+07;
m0 = -7.4807e+06;
g5 = -1.8497e+06;
g4 = -7.9658e+06;
g3 = -1.5743e+07;
g2 = -3.0234e+06;
g1 = -2.0215e+06;
g0 = -1.7686e+05;
R22 = 0.4752;
X = m0 + m1*t + m2*t.^2 + m3*t.^3 + m4*t.^4 + m5*t.^5 + m6*t.^6;
Y = g0 + g1*t + g2*t.^2 + g3*t.^3 + g4*t.^4 + g5*t.^5;
Z = X./Y
Z = 5×1
42.8435 -0.9336 -1.7141 7.8419 -23.5903
wc = sqrt(R22./Z);
wc(Z<0) = NaN; % mark out invalid results
td = (2./wc).*atan(wc.*t)
td = 5×1
-0.1900 NaN NaN 8.7695 NaN
tdmin = min(td(td>0)) % this would be the minimum positive value
tdmin = 8.7695
Suud Hasen
Suud Hasen el 3 de Jun. de 2021
After modified my actual model as per your earlier suggestions, it worked well. Much appreciated. Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 3 de Jun. de 2021

Comentada:

el 3 de Jun. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by