Search code examples
matlabgraphmatlab-figureprojectile

Projectile motion


I am supposed to write a script that provides multiple lines of projectile motion but my code doesn't seem to give me what I need.

disp('This program will calculate the trajectory of a ball thrown at an initial speed vo \n')
v0 = input('Please enter the initial speed');
x0 = 0;
y0 = 0;
g = 9.81;%m/s^2
T = 5 : 5 : 85;
vx = v0*cosd(T);
vy = v0*sind(T);
t = (2*v0.*sind(T))/g;
y = y0 + (vy.*t) - ((g.*(t.^2))/2);
x = x0 + vx.*t;
plot(x,y)

This is how the graph should look like:

expected output


Solution

  • In your code, T represents the initial degree. You want to calculate x and y for different initial degrees (5:5:85). Use a for loop for T and plot x and y for different t.

    disp('This program will calculate the trajectory of a ball thrown at an initial speed vo \n')
    v0 = input('Please enter the initial speed');
    x0 = 0;
    y0 = 0;
    g = 9.81;%m/s^2
    for T = 5 : 5 : 85
        vx = v0*cosd(T);
        vy = v0*sind(T);
        t = linspace(0,(2*v0.*sind(T))/g,100);
        y = y0 + (vy.*t) - ((g.*(t.^2))/2);
        x = x0 + vx.*t;
        plot(x,y)
        hold on
        xlim([-inf inf])
        ylim([-inf inf])
    end
    

    Output:

    This program will calculate the trajectory of a ball thrown at an initial speed vo \n
    Please enter the initial speed10
    

    enter image description here