MATLAB: How do you only plot a certain number of results

graph

This is my code and it doesnt work because there is more x values than y values because i didnt want the negative y values
clear all
close all
clc
v=2;
c=0.7;
theta=pi/4;
vx=v*cos(theta);
vy=v*sin(theta);
height=(vy^2)/(9.81);
time=(height/vy)*2;
d=time*vx;
vx2=vx*c;
vy2=vy*c;
height2=(vy2^2)/(9.81);
time2=(height/vy2)*2;
d2=time2*vx;
g=9.81;
inc=1/(1*10^4);
x=0:inc:d;
y=x*tan(theta)-((g*x.^2)/(2*(v^2)*cos(theta)^2));
x2=0;
x2=0:d2/1000:d2;
y2=0;
y2=x2*tan(theta)-((g*x2.^2)/(2*((v*c)^2)*cos(theta)^2));
ydata=y2(y2>=0);
x3=x2+d;
n = numel(ydata)
n2 = numel(x3)
hold
plot(x,y,x3,ydata)

Best Answer

Apply the same filtering to your x values:
xdata = x3(y2 > 0);
plot(x, y, xdata, ydata);