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
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
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!