Clear Filters
Clear Filters

for loop/indexing assistance for simple plot script

2 views (last 30 days)
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)

Accepted Answer

bym
bym on 9 Jul 2012
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

More Answers (1)

Joe
Joe on 9 Jul 2012
thanks!!!!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!