Multiple conditional statements and a for loop.

268 次查看(过去 30 天)
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 个评论
Harshit Jain
Harshit Jain 2019-2-7
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.

请先登录,再进行评论。

采纳的回答

Bob Thompson
Bob Thompson 2019-2-7
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by