Storing the value of an Array from a For loop with an If statement
14 次查看(过去 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!