function [y_values] = plotProjectile(vi, p_angle, t_values) syms t g = 9.81; % y = vi * sin(alpha)t - 0.5*g(t^2) y(t) = vi * sind(p_angle) * t - 0.5 * g * (t^2); y_values = double(subs(y, t, t_values)); plot(t_values, y_values, "Color", [1,0,0], "Marker","square"); ylim([0 max(y_values)]); xlim([0 max(t_values)]); title("Projectile Motion with Tangent Vectors"); xlabel("Time (seconds)"); ylabel("Height (meters)"); hold on % Getting derivative of y with respect to time to draw tanjent lines dy_dt = diff(y, t); dy_dt_values = double(subs(dy_dt, t, t_values)); line_length = (t_values(2) - t_values(1)); % Offset variable to make the text a little bit away from the % main graph to avoid overlapping vertical_offset = 0.2; for i = 1:length(t_values) %{ Both the vectors y, t have the same size, so we can get y(t) values at each drawn point by using index of that element in vector, which I called ( i ) in my example. %} t0 = t_values(i); y0 = y_values(i); slope = dy_dt_values(i); %{ line length should be equal square root of (dx)^2 + (dy)^2 and dy = slope * dx where slope = dy/dx therefore line length = dx * square root of (1 + slope^2) and multiplied by 5 for scaling on the plot %} dx = (line_length * 5 / sqrt(1 + slope^2)); quiver(t0, y0 , dx, slope * dx, 'b', 'LineWidth', 1, 'MaxHeadSize', 0.5); text(t0, y0 + vertical_offset, sprintf('v = %.2f', slope), ... 'FontSize', 8, 'Color', 'blue'); % Drawing vertical and horizontal at each point quiver(t0, y0, 0, dx * slope, 'k', 'LineWidth', 1, 'MaxHeadSize', 0.5); quiver(t0, y0, dx, 0, 'k', 'LineWidth', 1, 'MaxHeadSize', 0.5); end legend("Projectile Path", "Tangent Vectors (V)", "Vertical and Horizontal Components"); hold off end