Storing the value of an Array from a For loop with an If statement
24 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dunovan Mcguire
le 13 Fév 2021
Commenté : Dunovan Mcguire
le 14 Fév 2021
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 commentaires
Réponse acceptée
Walter Roberson
le 13 Fév 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 commentaires
Walter Roberson
le 14 Fév 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
Plus de réponses (1)
randerss simil
le 13 Fév 2021
tss(i)=t; %records time enter steady state.
Use the index to record the time entered
3 commentaires
randerss simil
le 13 Fév 2021
Modifié(e) : randerss simil
le 13 Fév 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))
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!