Storing the value of an Array from a For loop with an If statement

34 visualizaciones (últimos 30 días)
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 comentarios
Walter Roberson
Walter Roberson el 13 de Feb. de 2021
tss(end+1) = t;
instead of tss=t ?
Dunovan Mcguire
Dunovan Mcguire el 13 de Feb. de 2021
Editada: Dunovan Mcguire el 13 de Feb. de 2021
nothing. it's still not recording the tss value.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Feb. de 2021
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 comentarios
Walter Roberson
Walter Roberson el 14 de Feb. de 2021
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
Dunovan Mcguire
Dunovan Mcguire el 14 de Feb. de 2021
I only need the last tss but close enough for my purposes Thak you.

Iniciar sesión para comentar.

Más respuestas (1)

randerss simil
randerss simil el 13 de Feb. de 2021
tss(i)=t; %records time enter steady state.
Use the index to record the time entered
  3 comentarios
randerss simil
randerss simil el 13 de Feb. de 2021
Editada: randerss simil el 13 de Feb. de 2021
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))
Dunovan Mcguire
Dunovan Mcguire el 13 de Feb. de 2021
Editada: Dunovan Mcguire el 13 de Feb. de 2021
This is the output for Wn=1 Zeta=0.5. As you can see, the condition is activated at several points as the x value approachs 1 and tss is supposed to record the time of when x enters 2% of the value and hold it. The system then continues to loop for the remainder and is supposed to the record the final time the system enters 2% of 1 and remains there.
To simplify. tss is first to record 2,1 then as it leaves steady state p=1(false) and the if statement is primed to record the next time it enters 2% with is roughly 5, then eventually 8 which is the correct answer for this graph. this system is failing to record ANY tss value and the system is not recognizing tss as a variable.
I added tss=[] to prime the system to look for this variable but the the system is unable to find tss=t within the if statement, for some reason.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by