I'm trying to track a particle's velocity at each time step, but my X&Y Velocity Variables keep becoming matrices, instead of overwriting each time. How should I fix this?
13 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
cwmiller
il 26 Nov 2021
Risposto: Walter Roberson
il 27 Nov 2021
The code that keeps giving me and error is
Counter = Counter + 1
TrackedVelocity(Counter) = sqrt((VelocityX)^2+(VelocityY)^2);
5 Commenti
Walter Roberson
il 27 Nov 2021
Suppose ParticleMass is non-scalar, then the / operator would be matrix division rather than element-by-element division, and that could lead to unexpected results.
Risposta accettata
Walter Roberson
il 27 Nov 2021
TrackedVelocity(Counter) = sqrt((VelocityX)^2 + (VelocityY)^2);
So TrackedVelocity is a vector that is increasing in size.
if VelocityY < 0
DragForceY = -(DragCoefficient*(TrackedVelocity)*(VelocityY));
elseif VelocityY > 0
DragForceY = -(DragCoefficient*(TrackedVelocity)*(VelocityY));
end
You use all of the vector TrackedVelocity in DragForceY so DragForceY is going to increase in size as you go. That is going to trigger other variables to increase in size.
Question:
Your two if branches have the same code. Therefore the difference between doing the code unconditionally and doing the code the way you do it now, is in the behaviour if neither branch of the if is met -- which would occur if VelocityY == 0 exactly or VelocityY became NaN somehow. Are you sure that in either of those cases, that you want to leave DragForceY at its previous value ?
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!