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

조회 수: 1 (최근 30일)
Mikey
Mikey 2014년 4월 29일
편집: Mikey 2014년 4월 29일
  • 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