# Plotting 2 lines in the same graph

11 views (last 30 days)
Kyle Hamanne on 5 Jun 2020
Commented: Kyle Hamanne on 5 Jun 2020
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)
% 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
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
% 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
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
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

Shubhankar Poundrik on 5 Jun 2020
Hi Kyle,
I understand that you are trying to plot two plots in the same figure using the lines
plot(xCoordinates, yCoordinates);
xlabel('Distance (m)')
ylabel('Height (m)')
title('Ball Flight Path')
hold on
plot(xCoordinates_NOdrag, yCoordinates_NOdrag)
hold off
However, only one plot seems to be visible when the code is run.
The code for plotting the data is correct. However, all values contained in the arrays xCoordinates_NOdrag and yCoordinates_NOdrag are all equal.
Following are their values.
xCoordinates_NOdrag =
160.4920 160.4920 160.4920 160.4920 160.4920
yCoordinates_NOdrag =
-27.6045 -27.6045 -27.6045 -27.6045 -27.6045
Thus, plotting them creates a single point on the figure. It can be better seen by adding a circular marker to the plot.
Replace the above code with the following.
plot(xCoordinates, yCoordinates);
xlabel('Distance (m)')
ylabel('Height (m)')
title('Ball Flight Path')
hold on
plot(xCoordinates_NOdrag, yCoordinates_NOdrag, 'ro')
hold off
The problem seems to be with the values of the variables being plotted and not the code for plotting them.
Regards,
Shubhankar.
Kyle Hamanne on 5 Jun 2020
thankyou!

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
##### 2 CommentsShowHide 1 older comment
Kyle Hamanne on 5 Jun 2020
I tried altering those final sections, still only plotted 1 line?

R2020a

### Community Treasure Hunt

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

Start Hunting!