I have to plot 3 curves on a single graph.

m=0.000185; %mass of ball (slugs)
k=0.83; %spring constant (lb/ft)
theta=45; %launch angle (degrees)
hd=[7,8,9]; %horizontal distance required (ft)
g=32.2; %gravity (ft/s^2)
v0=sqrt((g*hd)/(2*sin(theta)*cos(theta))); %calculates initial velocity needed for the ball to hit 7,8, and 9 feet (ft/s^2)
s=v0*sqrt(m/k)*12; %distance the spring must be compressed in order to achieve the three initial velocities (in)
vx=v0*cos(theta); %initial velocity in the x direction (ft/s^2)
vy=v0*sin(theta); %initial velocity in the y direction (ft/s^2)
t=vy/g; %time the ball is in the air (s)
y=vy.*t-0.5*g.*t.^2; %maximum vertical distance achieved (ft)
x=2*vx.*t; %horizontal distance achieved (ft)
plot(x,y)
Here is my code so far. I need to plot the three curves on a single graph, but it just keeps outputting a straight line. Not really sure how to fix this.

4 Comments

what three series of data do you want to plot?
There are three values of v0, and each of those are divided into x and y components. The code outputs those no problem, but when I try and graph them with respect to time (the x and y equations), I just get a straight line, even though t is quadratic.
jonas
jonas on 3 May 2018
Edited: jonas on 3 May 2018
But, as far as I can see there is no vector with time, so you cannot plot the distance travelled over time. Each variable in your code are just single values. Also, shouldn't the last equations be x=vx*t, and y=vy*t/4?
So you are trying to plot the x and y values respect to time, where time starts at t=0 until t=vy/g, am I right?

Sign in to comment.

 Accepted Answer

Try something like this:
tf=vy/g; %time the ball is in the air (s)
t = (0:0.1:2)' * tf; % Create Matrix Of ‘t’ Values
y=vy.*t-0.5*g.*t.^2; %maximum vertical distance achieved (ft)
x=2*vx.*t; %horizontal distance achieved (ft)
plot(x,y)
Experiment with this approach to get the result you want.

2 Comments

That worked perfectly! Thank you!
My pleasure!
If my Answer helped you solve your problem, please Accept it!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!