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?

1 view (last 30 days)
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);
cwmiller on 27 Nov 2021
Here is this same code in question, but with my input parameters...
%% Input Parameter Settings
clear
clc
ParticleMass = 1; % Mass of particle (kg)
g = -9.81; % Gravitational acceleration (m/s^2)
DragCoefficient = 0.5;
DeltaT = 0.001;
EndTime = 1;
XInitial = 0; % (m)
YInitial = 0; % (m)
VelocityInitial = 34; % (m/s)
YVelocityInitial = 3; % (m/s)
XVelocityInitial = 5; % (m/s)
%% Model Initialization
Counter = 0;
TrackedDistanceTravelled = 0; %(m)
DragForceY = -DragCoefficient*(VelocityInitial)*(YVelocityInitial); % Is this the correct way to find Drag Force in the X & Y Direction?
DragForceX = -DragCoefficient*(VelocityInitial)*(XVelocityInitial); % ...We need it for when it just starts right?
% DragForceX/Y = DragCoefficient*(Velocity)*(X/YVelocity)
%% Time Marching
for t = 0:DeltaT:EndTime
% x,y Force and acceleration calculations
ForceY = (ParticleMass*g)+(DragForceY); % (N)
ForceX = (DragForceX); % (N)
AccelY = ForceY/ParticleMass; % (m/s^2)
AccelX = ForceX/ParticleMass; % (m/s^2)
% Numerical Calculations that update x,y position and velocity
Xposition = XInitial+(XVelocityInitial*(t))+(0.5*AccelX*(t)^2); % How would I set up an
Yposition = YInitial+(YVelocityInitial*(t))+(0.5*AccelY*(t)^2);
TrackedDistanceTravelled = TrackedDistanceTravelled + sqrt((Xposition)^2 + (Yposition)^2);
% equation to find position and
% velocity for the first
VelocityX = XVelocityInitial+(AccelX*(t)); % time-step??
VelocityY = YVelocityInitial+(AccelY*(t));
Counter = Counter +1;
TrackedVelocity(Counter) = sqrt((VelocityX)^2 + (VelocityY)^2);
DragForceX = -(DragCoefficient*(TrackedVelocity(Counter))*(VelocityX));
if VelocityY < 0
DragForceY = -(DragCoefficient*(TrackedVelocity)*(VelocityY));
elseif VelocityY > 0
DragForceY = -(DragCoefficient*(TrackedVelocity)*(VelocityY));
end
if Yposition < 0
break
end

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 ?