How to create a for loop to launch an object at an angle?

1 次查看(过去 30 天)
  • Im want to launch this star at an angle of 45degrees with 9 equally spaced coordinates for this trajectory, but i dont know how to write it in a for loop. *
starx = [10,9,4,8,6,10,14,12,16,11];
stary = [16,12,12,9,4,7,4,9,12,12];
hold on; grid on;
fill(starx,stary,'y');
axis equal
tt = linspace(0, 7.2, 9); %time
v0 = 50; %initial velocity
theta = 45*pi/180; %angle
y0 = 0; %height launched from ground
g = 9.81; %gravity
xx = v0*tt*cos(theta); %x(t) = v0*t*cos0
yy = y0 + v0*tt*sin(theta) - g*tt.^2 / 2; % y(t) = y0 + v0*t*sin0 - g*t^2/2

回答(1 个)

Image Analyst
Image Analyst 2014-4-29
Just put it in a for loop. Give an index to yy and tt so you're referring to just one element instead of the whole array like you did in your vectorized approach:
starx = [10,9,4,8,6,10,14,12,16,11];
stary = [16,12,12,9,4,7,4,9,12,12];
hold on;
grid on;
fill(starx,stary,'y');
axis equal
tt = linspace(0, 7.2, 9); %time
v0 = 50; %initial velocity
theta = 45*pi/180; %angle
y0 = 0; %height launched from ground
g = 9.81; %gravity
xx = v0*tt*cos(theta); %x(t) = v0*t*cos0
for t = 1 : length(tt)
yy(t) = y0 + v0 * tt(t) * sin(theta) - g * tt(t).^2 / 2; % y(t) = y0 + v0*t*sin0 - g*t^2/2
end
yy
plot(tt,yy, 'bo-', 'LineWidth', 3)
grid on;

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by