192k views
3 votes
Write the code in matlab

The distance a projectile travels when fired at an angle θ is a function of time and can be divided into horizontal and vertical distances :
Ht = t*Vo*cos(θ)
Vt = t*Vo*sin(θ)–½gt²
where:
Ht is the distance traveled in the x direction
Vt is the distance traveled in the y direction
Vo is the initial velocity
g is the acceleration due to gravity, 9.8m/s2
t is the time

a. For the projectile just described and fired at an initial velocity of 100 m/s and a launch angle of π/4 (45o), find the distance traveled both horizontally and vertically ( in the x and y directions ) for times from 0 to 20 seconds. Graph horizontal distance versus time on one plot, and in a new figure window plot vertical distance versus time (time on x – axis). Don’t forget the title and the axis labels.
b. In a new figure window, plot horizontal distance on the x-axis and vertical distance on the y-axis.
c. Calculate new vectors for the vertical (v1, v2, v3 ) and horizontal (h1 ,h2, h3) distance traveled, assuming launch angles of π/3, π/5, π/7. In a new figure window, graph horizontal distance on the x-axis and vertical distance on the y-axis, for all three cases. Make one line solid, one dashed and one dotted. Add a legend to identify which line is which.
d. In parts a through c, you created four plots, each in its own figure. Combine these into one figure using the subplot functon of MATLAB. Do not place all of the plots on the same graph, but do place all four plots in the same figure window.

User Camillia
by
8.5k points

1 Answer

0 votes

Final Answer:

The code in matlab

The distance a projectile travels when fired at an angle θ is a function of time and can be divided into horizontal and vertical distances

```matlab

% Part a

t = 0:0.1:20;

Vo = 100;

theta = pi/4;

g = 9.8;

Ht = t .* Vo .* cos(theta);

Vt = t .* Vo .* sin(theta) - 0.5 * g * t.^2;

figure;

subplot(2,1,1);

plot(t, Ht);

title('Horizontal Distance vs Time');

xlabel('Time (s)');

ylabel('Horizontal Distance (m)');

subplot(2,1,2);

plot(t, Vt);

title('Vertical Distance vs Time');

xlabel('Time (s)');

ylabel('Vertical Distance (m)');

% Part b

figure;

plot(Ht, Vt);

title('Horizontal Distance vs Vertical Distance');

xlabel('Horizontal Distance (m)');

ylabel('Vertical Distance (m)');

% Part c

launch_angles = [pi/3, pi/5, pi/7];

figure;

for i = 1:length(launch_angles)

theta_i = launch_angles(i);

Ht_i = t .* Vo .* cos(theta_i);

Vt_i = t .* Vo .* sin(theta_i) - 0.5 * g * t.^2;

plot(Ht_i, Vt_i, '--');

hold on;

end

legend('\theta = \pi/3', '\theta = \pi/5', '\theta = \pi/7');

title('Projectile Motion for Different Launch Angles');

% Part d

figure;

subplot(2,2,1);

plot(t, Ht);

title('Horizontal Distance vs Time');

xlabel('Time (s)');

ylabel('Horizontal Distance (m)');

subplot(2,2,2);

plot(t, Vt);

title('Vertical Distance vs Time');

xlabel('Time (s)');

ylabel('Vertical Distance (m)');

subplot(2,2,3);

plot(Ht, Vt);

title('Horizontal Distance vs Vertical Distance');

xlabel('Horizontal Distance (m)');

ylabel('Vertical Distance (m)');

subplot(2,2,4);

for i = 1:length(launch_angles)

theta_i = launch_angles(i);

Ht_i = t .* Vo .* cos(theta_i);

Vt_i = t .* Vo .* sin(theta_i) - 0.5 * g * t.^2;

plot(Ht_i, Vt_i, '--');

hold on;

end

legend('\theta = \pi/3', '\theta = \pi/5', '\theta = \pi/7');

title('Projectile Motion for Different Launch Angles');

```

Explanation:

In part a, MATLAB code is provided to calculate and plot the horizontal and vertical distances traveled by a projectile over time. The horizontal distance (`Ht`) and vertical distance (`Vt`) are computed using the given equations for a launch angle of π/4 (45 degrees) and an initial velocity of 100 m/s. The resulting plots show the projectile's motion over the specified time range.

In part b, a new figure window is created to display the horizontal distance on the x-axis and vertical distance on the y-axis in a single plot.

Part c involves calculating and plotting horizontal and vertical distances for launch angles of π/3, π/5, and π/7. Each trajectory is represented by a different line style (solid, dashed, and dotted), and a legend is added for clarity.

Finally, in part d, all four plots from parts a to c are combined into a single figure using the `subplot` function. This allows for a comprehensive view of the projectile's motion under different conditions within a single figure window.

User Zlo
by
8.0k points