필터 지우기
필터 지우기

for loop/indexing assistance for simple plot script

조회 수: 2 (최근 30일)
Joe
Joe 2012년 7월 9일
hey everyone,
I'm working on a hmwk assignment where I have to use function handles to create points for a plot. I'm allowed 1 for loop to run through an array A for different angles, which I then calculate and plot trajectories for. here is my code, currently I get a plot of one trajectory, and it doesn't seem to run through all the values of A, only the first. any help would be appreciated.
clc clear
%constants
g= 1.62;%m/s v= 10; %m/s A= [15 30 45 60 75]'; %degrees
%functions with handles tof,h,x tof=@(A)(2*v/g)*sind(A); %time of flight h=@(t,A)v.*t*sind(A)-.5*g*(t.^2); %height of ball x=@(t,A)v.*t*cosd(A); %horizontal distance
hold all for i=1:length(A) ts=tof(A(i)); t=linspace(0,ts,30); H=h(t(:),A(i)); X=x(t(:),A(i));
end plot(X,H)

채택된 답변

bym
bym 2012년 7월 9일
Move your plot command to inside the loop, as in:
clc; clear
%constants
g= 1.62;%m/s
v= 10; %m/s
A= [15 30 45 60 75]'; %degrees
%functions with handles tof,h,x
tof=@(A)(2*v/g)*sind(A); %time of flight
h=@(t,A)v.*t*sind(A)-.5*g*(t.^2); %height of ball
x=@(t,A)v.*t*cosd(A); %horizontal distance
hold all
for i=1:length(A)
ts=tof(A(i));
t=linspace(0,ts,30);
H=h(t(:),A(i));
X=x(t(:),A(i));
plot(X,H)
end

추가 답변 (1개)

Joe
Joe 2012년 7월 9일
thanks!!!!

카테고리

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