MATLAB: How to fix the code so that the x and y equations run

error

g=9.81
v0=5
theta0=15:15:75
t=0:(1/128):3
x=v0.*cosd(theta0).*t
y=v0.*sind(theta0).*t-(0.5.*g.*t.^2)
This is the code I have now and I'm having trouble getting the x and y equations to work.
Please help. Thanks in advance.

Best Answer

If y is not allowed to go negative, alter my other solution and use clipping to zero. Note how the farthest distance it goes is when the angle is at 45 degrees, which makes intuitive sense.
% Initialization / clean-up code.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 20;
g = 9.81;
v0 = 5;
t = 0 : (1/128) : 3;
for theta0 = 15:15:75
x = v0 .* cosd(theta0) .* t;
y = v0 .* sind(theta0) .* t - (0.5 * g * t.^2);
% Don't let y go negative:
y = max(y, 0);
plot(x, y, '-', 'LineWidth', 2);
hold on;
end
grid on;
xlim([0,3]);
% Draw x axis:
line(xlim, [0,0], 'Color', 'k', 'LineWidth', 2);
legend('15', '30', '45', '60', '75');
title('Y vs. X', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')