Filter löschen
Filter löschen

for loop/indexing assistance for simple plot script

2 Ansichten (letzte 30 Tage)
Joe
Joe am 9 Jul. 2012
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)

Akzeptierte Antwort

bym
bym am 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

Weitere Antworten (1)

Joe
Joe am 9 Jul. 2012
thanks!!!!

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