MATLAB: I need help on how to plot the max y peak on two different lines on one plot

y peak plotting

thank you for any help in adavnce.
% This program computes three projectiles motion, velocity, and accleration of a sphere with and without
%air resistance
clear;
close all;
clc
%% Sphere soccer ball
rad = 0.037; % sphere radius
mass = 0.147; % mass spherekg
Cd = 0.47; % Drag coefficient
Af = pi*rad^2; % cross section area sphere
%% air density Gravitiy
rho = 1.2; % air density
g = 9.81; % gravitational acceleration
%% Initial condition: position and velocity
yo = 2; % initial position
V1 = 15; % initial velocity
xo = 0; %intial position
d=45 ; %degreess
theta = d*pi/180;
%% Time vector and time step
dt = 0.1; % time step
t = 0:dt:10; % time vector
imax = length(t);
%% Velocity and position no drag
acc1 = -g; % acceleration with no drag
x = V1*t*cos(theta); % position no drag
y = V1*t*sin(theta)- 0.5*g*t.^2;
vx = V1 * cos(theta); % velocity no drag
vy = V1 * sin(theta)-0.5*g*t.^2;
V1 = sqrt(vx^2 +vy.^2);
%% terminal velocity with drag
Vter = sqrt(mass*g/(0.5*Cd*rho*Af));
%% Acceleration velocity and position with drag
%% Pre set
accx2 = zeros(size(t)); % initialize acceleration with drag
accy2 = zeros(size(t));
Fdx = zeros(size(t)); % initialize force with drag
Fdy = zeros(size(t));
Vx2 = zeros(size(t)); % initialize velocity with drag
Vy2 = zeros(size(t));
V2 = zeros(size(t));
y2 = zeros(size(t)); % initialize position with drag
x2 = zeros(size(t));
% Initialize previous state
yprv = y; % initialize previous position (initial condition)
xprv = x;
Vxprv = vx; % initialize previous velocity (initial condition)
Vyprv = vy;
%% loop drag force, acceleration, velocity and position
for i=1:imax
Fdx(i) = 0.5.*Cd.*rho.*Af.*Vxprv.^2; % Drag force
Fdy(i) = 0.5.*Cd.*rho.*Af.*Vyprv(i).^2;
accx2(i) = -Fdx(i)/mass*V2(i).*Vx2(i) ; % Acceleration
accy2(i) =-g - Fdy(i)/mass*V2(i).*Vy2(i) ;
V2(i)=sqrt(Vx2(i).^2+Vy2(i).^2);
Vx2(i) = Vxprv + accx2(i)*dt; % Velocity
Vy2(i) = Vyprv(i) + accy2(i)*dt;
x2(i) = xprv(i) + Vx2(i)*dt+0.5*accx2(i)*dt.^2;
y2(i) = yprv(i) + Vy2(i)*dt+0.5*accy2(i)*dt.^2; % Position
yprv(i) = y2(i); % Update previous position
xprv(i) = x2(i);
Vxprv = V2(i); % Update previous velocity
Vyprv(i) = V2(i);
end
%% drag no drag
figure(1)
plot(x,y,'b:'), hold on
plot(x2,y2,'g'),

Best Answer

  • [maxy, maxyidx] = max(y);
    [maxy2, maxy2idx] = max(y2);
    plot(x(maxyidx), maxy, 'b^');
    plot(x2(maxy2idx), maxy2, 'g^');