Creating a Function to Plot Projectile with Drag

Hello, I've recently been tasked with creating a program for a course regarding plotting projectile motion with drag (air resistance). The problem i have run into is regarding overwriting of variable (Vx and Vy) but whenever i replace them and put them back into the function so they don't overwrite themselves the program either does nothing or crashes matlab. since i don't have matlab at home i've been using freelab and convert back and forth as i work at uni and home.
Another problem i have had is that i am unable to plot the y axis for the function without drag.
clear
close all
disp('Welcome to the Projectile Motion Plotter');
disp('This projects the motion for a tennis ball with and without air resistance');
Vx = input('Please input the horizontal velocity [m/s]: ');
Vy = input('Please input the vertical velocity [m/s]: ');
%Sets up intial conditions
V = sqrt(Vx^2 + Vy^2); %Determines V by combining v of both axes
G = 9.80665; %m/s^2 Acceleration due to Gravity
DC = 0.8; %Drag coefficient
Area = 0.0033; %m^2 cross section area of a tennis ball
Mass = 0.057; %Kg mass of tennis ball
x(1) = 0; %intial x postion
y(1) = 0; %inital y postion
xf(1) = 0; %inital xf postion
yf(1) = 0; %intial yf postion
AP = 1.2; %kg/m^3 Air Density @ Sea Level
D = AP*DC*Area/2; %constant needed for drag calculations created
t(1) = 0; %sets intial time
dt = 0.01; %s set the intervals at which time will be evalutated
i = 1; %sets counter/index
%Starts a loop for Projectile Motion with Drag
while min(y)> -0.01;
t = t + dt;
i = i + 1;
xf(i) = xf(i-1)+ Vx.*dt;
AxD = - ( D / Mass ) * V * Vx;
AyD = -G - ( D / Mass ) * V * Vy;
Vx = Vx + AxD * dt;
Vy = Vy + AyD * dt;
x(i) = x(i-1) + Vx * dt + 0.5 * AxD * dt^2;
y(i) = y(i-1) + Vy * dt + 0.5 * AyD * dt^2;
end;
plot(x,y,'b'), hold on; %plots the Projectile Motion with Drag
plot(xf,y,'r'), hold off; %plots the Projectile Motion without Drag
xlabel('Horizontal Distance (m)'); %labels the x axis "Horizontal Distance (m)"
ylabel('Vertical Distance (m)'); %Labels the y axis "Vertical Distance (m)"
title('Projectile Motion Paths'); %Gives a Title "Projectile Motion Paths"
so this is the latest and furthermost i have got, this was done in freeMat 4.2 so some small changes need to be done to make it work in matlab. Other things i have yet to do is add the y axis for projectile motion without drag and a legend for the graph. if you spot anything odd or can tell me how to stop getting those variables overwriting themselves i would be very thankful.
Bryce

6 个评论

A possible solution: create two differently named variables
V_old, V_new
and replace where it corresponds.
what do you mean that Vx is overwritten? Do you mean that:
Vx = Vx + AxD * dt;
should use Vx inputted by the user on the right side?
Sara: i tried using what Jose-Luis said by replacing the Vx on the left hand side with Vx_New and and the Vx on the right hand side with Vs_Old and changing all associated variables. The problem i had was that after the point where:
Vx = Vx + AxD * dt;
was written the Vx was not the original value, so afterwards it was unusable as the variable which the user inputted. I changed it to now:
Vx_New = Vx_Old + AxD * dt;
Vy_New = Vy_Old + AyD * dt;
x(i) = x(i-1) + Vx_New * dt + 0.5 * AxD * dt^2;
y(i) = y(i-1) + Vy_New * dt + 0.5 * AyD * dt^2;
the new problem is now it doesn't work, whereas before if i just let it overwrite Vx it would work but after that Vx could not be used anymore as it was a different value. The problem it comes up with now is that it fails "array indexing scalar values...", "index out of range"
Please try using the debugger.
dbstop if error
your_script.m
And look at the variables in your workspace. It would be much more efficient than us trying to debug remotely.
well thanks for all your help, very much appreciated
Substitute V for Vx in this loop: xf(i) = xf(i-1)+ Vx.*dt;

请先登录,再进行评论。

回答(1 个)

You can replace your while loop with:
while min(y)> -0.01;
t = t + dt;
i = i + 1;
xf(i) = xf(i-1)+ Vx.*dt;
AxD = - ( D / Mass ) * V * Vx;
AyD = -G - ( D / Mass ) * V * Vy;
Vx_new = Vx + AxD * dt;
Vy_new = Vy + AyD * dt;
x(i) = x(i-1) + Vx_new * dt + 0.5 * AxD * dt^2;
y(i) = y(i-1) + Vy_new * dt + 0.5 * AyD * dt^2;
Vx = Vx_new;
Vy = Vy_new;
end;
It plots something without errors.

2 个评论

Thanks a Bunch for fixing that part!!!! Now it works perfectly and i can keep the original values for later functions :)
Note that Vx and Vy are still overwritten at each iteration. To keep the user inputted values, define two new variables, e.g., V0 = Vx before the while loop.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Physics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by