Indexing in a while loop
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Alexander Wilcox
le 30 Jan 2019
Réponse apportée : Walter Roberson
le 30 Jan 2019
I need the results to be the length until mfuel is equal to or less than 0. Please help.
time(i)=0;
dt=0.01; % change in time
height(i)=1;% elevation
i=1;
BR=4.8; % burn rate
mfuel(i)=134; % mass fuel
mtot=150; % total mass
mcase=16; % mass of casing
fthrust(i)=1560; % force of thruse
athrust(i)=0; % thrust acceleration
ag=-9.81; % acceleration of gravity
atot(i)=athrust(i)-ag; % total acceleration
v(i)=0; % velocity
ke(i)=0; % kinetic energy
pe(i)=0; % potential energy
te(i)=0; % total energy
while height > 0
time(i+1)=time(i)+dt;
mfuel(i+1)=max((mfuel(i)-BR*dt),0); % add if statement to make sure mfuel isnt <0
mtot(i+1)=mfuel(i+1)+mcase;
fthrust(i+1)=1560*sign(mfuel(i+1)); % -1 if neg 0 if zero 1 is pos
athrust(i+1)=fthrust(i+1)/mtot(i+1);
v(i+1)=v(i)+((atot(i+1)+atot(i))/2)*dt;
height(i+1)=height(i)+((v(i+1)+v(i))/2)*dt;
ke(i+1)=1/2*mtot(i)*v(i)^2;
pe(i+1)=mtot(i)*(-g)*height(i);
te(i+1)=pe(i)+ke(i);
end
0 commentaires
Réponse acceptée
Walter Roberson
le 30 Jan 2019
Your line
atot(i)=athrust(i)-ag; % total acceleration
does not define an equation. It is executed only once, with the current value of i, so atot becomes a scalar.
Then in your line
v(i+1)=v(i)+((atot(i+1)+atot(i))/2)*dt;
on the first iteration you have the problem that atot(1+1) does not exist.
After your while loop,
nofuel_pos = find(mfuel <= 0, 1);
and then you can index your arrays at nofuel_pos or nofuel_pos - 1 as appropriate.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing 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!