How do I fix my code so that the x and y equations run?

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.

 Accepted 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')

More Answers (2)

theta0 and t don't have the same number of elements. What are you trying to do?
Do you want theta0 and t to both track each other, like use theta0(1) when you use t(1), use theta0(2) when you use t(2), etc.? In that case theta0 and t must be the same length.
Or do you want to create an image where you have theta0 along the y direction and t along the x direction so that you have every possible combination of theta0 with t? In that case you can have theta0 and t be different lengths but you'll have to have two for loops, one over theta0 and one inside over t. Or you can use meshgrid() if you want a vectorized solution.
To answer we need to know which way you want it.

2 Comments

I want the x equation on the x-axis and the y equation on the y-axis, essentially plugging in the variables and plotting them. I understand that they do not have the same number of elements, I am just not sure how to approach the problem to fix it.
I really don't know what that means. You have a time axis, and an x coordinate for each time, and a y coordinate for each time. It doesn't make sense to say you want the x equation along the x axis and y equation along the y axis. Perhaps if you draw a diagram of what you expect to see.

Sign in to comment.

Try this:
% 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);
plot(x, y, '-', 'LineWidth', 2);
hold on;
end
grid on;
% 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')

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!