Storing the value of an Array from a For loop with an If statement
조회 수: 8 (최근 30일)
이전 댓글 표시
Hello, I'm currently attmepting to use an if statemtn to store the info of my loop with certain criteria. the for loop cycles 200 times and if at any point if the cycle provides a value within 2% of the value 1, the code is to store it as value tss and set my flag as true. It only records the first time it happens in a series. if the loop ever than outputs something outside 2% of 1 I set flag false then keep doing till the system records a tss that never leaves within 2% of 1.
The problem is, tss isn't recording a value, I need some assistance. here is the code.
p=1; %Flag 9p) set as 1(false)
prompt= 'Please input natural frequency'; %System asking for input of Natural frequency (Wn)
Wn=input(prompt);
prompt='please input a Zeta value between 0 and 1'; %System asking for input Zeta (Z)
Z=input(prompt);
while Z<=0 || Z>=1 %While statement checks for Z value
prompt='please input a Zeta value between 0 and 1'; %as a value outside 0 or 1
Z=input(prompt); %works to correct if provide it
end
x=zeros(200,1); %Sets array to 200 at steps of 1
t=zeros(200,1); %Sets array to 200 at steps of 1
tmax=8/(Z*Wn); %tmax based off of Zeta and Natual frequency
dt=tmax/200; %dt as the intervals of the graph determined by max value and steps.
tss=[];
for i = 1:200 %sets value for for statement to loop 200 times.
t(i)=dt*(i-1); %caluculates t for each value of the array
x(i)=sec_ord(Wn,Z,t(i)); %calculates x for each value of the array
if 0.98<x&x<1.02 %if statement checks for 2% of steady-State
if p==1 %checks flage(p) for flase reading (1)
p=0; %sets p for 0(true)
tss=t; %records time enter steady state.
t=t+dt; %increments t to the next value of the increment
elseif p==0 %checks for true flag
t=t+dt; %increments t to the next value of the increment
else %if other factors aren't satified the following occurs
p=1; %flag is set to false
t=t+dt; %increments t to the next value of the increment
end
end
end
Any help would be great, the sooner the better.
댓글 수: 2
채택된 답변
Walter Roberson
2021년 2월 13일
if 0.98<x&x<1.02 %if statement checks for 2% of steady-State
x is a vector. That is a vector test, equivalent in MATLAB as-if you had tested
if all(0.98<x&x<1.02) %if statement checks for 2% of steady-State
You should only be testing x(i) at that point.
댓글 수: 5
Walter Roberson
2021년 2월 14일
p=1; %Flag 9p) set as 1(false)
prompt= 'Please input natural frequency'; %System asking for input of Natural frequency (Wn)
Wn=input(prompt);
prompt='please input a Zeta value between 0 and 1'; %System asking for input Zeta (Z)
Z=input(prompt);
while Z<=0 || Z>=1 %While statement checks for Z value
prompt='please input a Zeta value between 0 and 1'; %as a value outside 0 or 1
Z=input(prompt); %works to correct if provide it
end
x=zeros(200,1); %Sets array to 200 at steps of 1
t=zeros(200,1); %Sets array to 200 at steps of 1
tmax=8/(Z*Wn); %tmax based off of Zeta and Natual frequency
dt=tmax/200; %dt as the intervals of the graph determined by max value and steps.
tss=[];
for i = 1:200 %sets value for for statement to loop 200 times.
t(i)=dt*(i-1); %caluculates t for each value of the array
x(i)=sec_ord(Wn,Z,t(i)); %calculates x for each value of the array
if 0.98<x(i)&&x(i)<1.02 %if statement checks for 2% of steady-State
if p==1 %checks flage(p) for flase reading (1)
p=0; %sets p for 0(true)
tss(end+1)=t(i); %#ok<SAGROW> %records time enter steady state.
end
else
%not in steady state
p=1; %flag is set to false
end
end
추가 답변 (1개)
randerss simil
2021년 2월 13일
tss(i)=t; %records time enter steady state.
Use the index to record the time entered
댓글 수: 3
randerss simil
2021년 2월 13일
편집: randerss simil
2021년 2월 13일
May be the if condition in x is not true. That's why it's not entering if loop and _tss_is not recorded. What is output of function
x(i)=sec_ord(Wn,Z,t(i))
참고 항목
카테고리
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!