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

1 Ansicht (letzte 30 Tage)
Mikey
Mikey am 29 Apr. 2014
Bearbeitet: Mikey am 29 Apr. 2014
  • 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

Antworten (1)

Image Analyst
Image Analyst am 29 Apr. 2014
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;

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by