Creating a Function to Plot Projectile with Drag

60 Ansichten (letzte 30 Tage)
Bryce
Bryce am 5 Jun. 2014
Kommentiert: Douglas Leaffer am 25 Nov. 2017
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 Kommentare
Bryce
Bryce am 5 Jun. 2014
well thanks for all your help, very much appreciated
Douglas Leaffer
Douglas Leaffer am 25 Nov. 2017
Substitute V for Vx in this loop: xf(i) = xf(i-1)+ Vx.*dt;

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Sara
Sara am 5 Jun. 2014
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 Kommentare
Bryce
Bryce am 5 Jun. 2014
Thanks a Bunch for fixing that part!!!! Now it works perfectly and i can keep the original values for later functions :)
Sara
Sara am 5 Jun. 2014
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by