MATLAB: Projectile Motion: How can i stop this plot on y=50

function [ trajectory ] = project_Q2( velocity_I, launch_angle )
initial_x = 50; %initial position
initial_y = 100;
gravity = -9.81;
time = linspace(0,10); %time vector
theta = launch_angle * pi/180
velocity_x = velocity_I * cos(theta); %velocity equations
velocity_y = velocity_I * sin(theta);
x = initial_x + velocity_x * time; %plot x and y
y = initial_y + velocity_y * time + (0.5 * gravity * time.^2);
disp(x); disp(y);
plot(x, y, 'r', 'LineWidth', 1.5);
grid on;
hold on;
max_x = [0 1000]; %ground line
max_y = [50 50];
plot(max_x, max_y, 'k-', 'LineWidth', 1.5);
x1 = x(end); %'end' is the last number along that matrice
y1 = y(end);
x2 = x(end-1); %'end-1' is the number before final number in matrice
y2 = y(end-1);
x3 = 100;
y3 = 50;
x4 = 900;
y4 = 50;
point_intersection_x =((x4-x3)*(x2*y1-x1*y2)+(x2-x1)*(x3*y4-x4*y3))/((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
point_intersection_y =((y4-y3)*(x2*x1-x1*y2)+(y2-y1)*(x3*y4-x4*y3))/((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
fprintf('x co-ordinate for the point of intersection : %6.3f\n', point_intersection_x );
fprintf('y co-ordinate for the point of intersection : %6.3f\n', point_intersection_y );
end
This is the function i have created, what i need is the plot to stop on the ground line for any velocity/angle that is input into the function…Any idea??

Best Answer

Just after calculating x and y, you could put in these lines:
neg_y = y<0;
x(neg_y) = [];
y(neg_y) = [];
You might also want to use a finer time granularity, such as
time = linspace(0,10,1000); %time vector
so that you don't have a big gap when your final time point is still a ways from y==0.