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?
12 views (last 30 days)
Show older comments
cwmiller
on 26 Nov 2021
Answered: Walter Roberson
on 27 Nov 2021
The code that keeps giving me and error is
Counter = Counter + 1
TrackedVelocity(Counter) = sqrt((VelocityX)^2+(VelocityY)^2);
5 Comments
Walter Roberson
on 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.
Accepted Answer
Walter Roberson
on 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 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!