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 ビュー (過去 30 日間)
The code that keeps giving me and error is
Counter = Counter + 1
TrackedVelocity(Counter) = sqrt((VelocityX)^2+(VelocityY)^2);
  5 件のコメント
Walter Roberson
Walter Roberson 2021 年 11 月 27 日
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.
cwmiller
cwmiller 2021 年 11 月 27 日
編集済み: Walter Roberson 2021 年 11 月 27 日
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
Walter Roberson 2021 年 11 月 27 日
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 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by