Plotting 2 lines in the same graph
Show older comments
How can i get the 2 plots to come out on the same figure?
%% Problem parameters
% Projectile
m = 0.05; % Mass of projectile (kg)
r = 0.03; % Radius of projectile (m)
theta = 25; % Initial angle (degrees)
y0 = 50; % Initial hieght (m)
x0 = 0; % Initial distance (m)
vu = 50; % Initial velocity (m/s)
vu_NOdrag = 50; % Initial velocty with no drag (m/s)
g = -9.81; % Gravity force (m/s^2)
% Drag
p = 1.183; % Density of air (kg/m^3)
Cd = 0.08; % Drag coefficeint
A = pi*r^2; % Area
% Initial conditions
x = x0;
y = y0;
x_NOdrag = x0;
y_NOdrag = y0;
ax = 0;
ay = 0;
ax_NOdrag = 0;
ay_NOdrag = 0;
sig = ((theta)*pi)/180; % converting degrees to radians
t = 0
t_NOdrag = 0
% Instantanious angle (changes with time)
RADangle = sig;
RADangle_NOdrag = sig;
% Arrays to store x and y values for each step
xCoordinates = [];
yCoordinates = [];
xCoordinates_NOdrag = [];
yCoordinates_NOdrag = [];
% Weight force (ball)
fg = -9.81 * m;
% Time step, the smaller the time step the more accurate the approximation.
% (closer to the analytical solution)
dt = 1;
dt_NOdrag = 1;
while y >= 0
fd = -0.5 * Cd * A * (vu^2); % DRAG SOLUTION
% Equations of motion
% Drag
fx = fd * cos(RADangle);
fy = fd * sin(RADangle) + fg;
% Acceleration components
% Drag
ax = fx/m;
ay = fy/m;
% Calculating velocity components using acceleration components
% Drag
vx = (vu*cos(RADangle) + (ax * dt));
vy = (vu*sin(RADangle) + (ay * dt));
% Define magnitude of velocity using velocity components
% Drag
vu = sqrt((vx^2) + (vy^2));
% Define angle using velocity components
% Drag
RADangle = atan2(vy,vx);
% Calculating possition using velocity and acceleration componets,
% and sort componets to respective lists
x = x + vx * dt + .5*ax*(dt^2);
xCoordinates = [xCoordinates x];
y = y + vy * dt + .5*ay*(dt^2);
yCoordinates = [yCoordinates y];
% Time increasing by incriments of the chosen time step
t = t + dt;
end
while y_NOdrag>=0
fd_NOdrag = 0; % NO DRAG SOLUTION
% No drag
fx_NOdrag = fd_NOdrag * cos(RADangle);
fy_NOdrag = fd_NOdrag * sin(RADangle) + fg;
% No drag
ax_NOdrag = fx_NOdrag/m;
ay_NOdrag = fy_NOdrag/m;
% No drag
vx_NOdrag = (vu_NOdrag*cos(RADangle_NOdrag) + (ax_NOdrag * dt_NOdrag));
vy_NOdrag = (vu_NOdrag*sin(RADangle_NOdrag) + (ay_NOdrag * dt_NOdrag));
% No drag
vu_NOdrag = sqrt(((vx_NOdrag)^2) + ((vy_NOdrag)^2));
% No drag
RADangle_NOdrag = atan2(vy_NOdrag,vx_NOdrag);
x_NOdrag = x_NOdrag + vx_NOdrag * dt_NOdrag + .5*ax_NOdrag*(dt_NOdrag^2);
xCoordinates_NOdrag = [xCoordinates_NOdrag x];
y_NOdrag = y_NOdrag + vy_NOdrag * dt_NOdrag + .5*ay_NOdrag*(dt_NOdrag^2);
yCoordinates_NOdrag = [yCoordinates_NOdrag y];
t_NOdrag = t_NOdrag + dt_NOdrag;
end
% Displaying results
disp(['Time taken to reach ground = ', num2str(t), ' seconds'])
disp(['Total distance travelled = ', num2str(x), 'metres'])
disp(['Time taken to reach ground (with NO drag) = ', num2str(t), ' seconds'])
disp(['Total distance travelled (with NO drag) = ', num2str(x_NOdrag), 'metres'])
plot(xCoordinates, yCoordinates);
xlabel('Distance (m)')
ylabel('Height (m)')
title('Ball Flight Path')
hold on
plot(xCoordinates_NOdrag, yCoordinates_NOdrag)
hold off
Accepted Answer
More Answers (1)
Edgar Guevara
on 5 Jun 2020
Modify the last lines of your code:
disp(['Time taken to reach ground (with NO drag) = ', num2str(t), ' seconds'])
disp(['Total distance travelled (with NO drag) = ', num2str(x_NOdrag), 'metres'])
hold on
plot(xCoordinates, yCoordinates, 'ro-');
xlabel('Distance (m)')
ylabel('Height (m)')
title('Ball Flight Path')
plot(xCoordinates_NOdrag, yCoordinates_NOdrag, 'kx-')
legend({'Drag' 'No Drag'})
hold off
Categories
Find more on Line Plots 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!