Azzera filtri
Azzera filtri

Multiple conditional statements and a for loop.

268 visualizzazioni (ultimi 30 giorni)
i have a for loop that calls a function and the if statements are supposed to determine when the function gets within a certain range, byut i am unsure if i need the statements to be nested within each other it to split them up. heres some of the code:
for i = 1:npoints
t(i)=(i-1)*tmax/npoints;
x(i) = sec_ord(wn, zeta, t(i));
end
if sec_ord(wn, zeta, t(i))<1.02
if sec_ord(wn, zeta, t(i))>0.98
if flag==1
t=t+inc;
elseif flag==0
flag=1;
tss=t;
t=t+inc;
if t<=tmax
sec_ord(wn, zeta, t(i));
elseif flag==1
tss=set_time;
end
end
else
flag=0;
t=t+inc;
end
end
after this runs i want to be able to use some of the values(such as tss and set_time) set inside the if statements but they do not get stored.
Any help would be appreciated,
thanks.
  1 Commento
Harshit Jain
Harshit Jain il 7 Feb 2019
Just wanted to ask if the "if" statement is even working, since it is outside the "for" loop and t(i) would give error there. Also, I would appreciate if you would elaborate the question a little bit more.

Accedi per commentare.

Risposta accettata

Bob Thompson
Bob Thompson il 7 Feb 2019
You can combine multiple conditions using & (and) and | (or) (&& and || are also things). Using this method I think it's worth combining the first two levels of your if statement.
if sec_ord(wn, zeta, t(i))<1.02 & sec_ord(wn, zeta, t(i))>0.98
For the flag condition I would suggest leaving it nested because you have multiple conditions you're considering, and I find it slightly easier to comprehend when considered separate from the range condition.
Because you are calling t(i) within the if statements they will need to be nested inside for loop for a unique check each time you run the loop. As your code is currently written the t(i) within the if statements will simply be evaluated as t(npoints).
after this runs i want to be able to use some of the values(such as tss and set_time) set inside the if statements but they do not get stored.
If by 'not get stored' you mean that you only receive a single output value there are two reasons for this. The first is because the if statement is outside the for loop, and so only gets evaluated once, as already mentioned. The second is because you have not indexed tss or set_time, so the variables will simply contain a single value that gets overwritten each time they are calculated.
If any of these are not the answers you were looking for please feel free to expand on your questions.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by