How to plot two trajectories in Matlab?

8 ビュー (過去 30 日間)
Sarah Hamdan
Sarah Hamdan 2020 年 4 月 29 日
編集済み: Mrutyunjaya Hiremath 2020 年 5 月 2 日
I'm trying to plot two different trajectories on the same plot, but it's not working and the graphs are sitting in the III and IV quadrant for some reason. Why Isn't it working?
T=[0:10:3000];
function [xa,ya]= trajectory(VoA,ThetaA,T,g)
%Use formula (3) for xa
xa=VoA*cosd(ThetaA)*T;
%Use formula (1) for ya
ya=(VoA*sind(ThetaA)*T)-(0.5*g*T.^2)+2000;
plot(xa,ya)
end
[xa,ya]= trajectory(VoA,ThetaA,T,g)
hold on
function [xb,yb]= trajectory(VoB,ThetaB,T,g)
%Use formula (1) for yb
yb=(VoB*sind(ThetaB))*T-(0.5*g*T.^2);
%Use formula (5) for xb
xb=xoa-(VoB*cosd(ThetaB)*T);
plot(xb,yb)
end
[xb,yb]= trajectory(VoB,ThetaB,T,g)
P= InterX([xa;ya],[xb;yb])
  2 件のコメント
Mrutyunjaya Hiremath
Mrutyunjaya Hiremath 2020 年 4 月 29 日
@ Sarah Hamdan,
VoA, ThetaA and VoB, ThetaB.. where are you defining them?
Mrutyunjaya Hiremath
Mrutyunjaya Hiremath 2020 年 4 月 30 日
編集済み: Mrutyunjaya Hiremath 2020 年 5 月 2 日
@ Sarah Hamdan
Ok study the attached code.

サインインしてコメントする。

回答 (1 件)

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath 2020 年 4 月 29 日
編集済み: Mrutyunjaya Hiremath 2020 年 4 月 29 日
Hello Sarah Hamdan,
  1. Don't paste code as a image, use option 'Insert aline code'
  2. Don't 2 different function body with same function name .
  3. Don't use both script and function in same file.
Here is the solution ...
% 521560-how-to-plot-two-trajectories-in-matlab
function MA521560
[xA, yA] = trackA;
plot(xA,yA);
hold on;
[xB, yB] = trackB;
plot(xB,yB);
P = InterX([xA;yA],[xB;yB]);
plot(xA,yA,xB,yB,P(1,:),P(2,:),'ro');
hold off;
end
function [xA, yA] = trackA
xA = randi(10,[1,10]);
yA = randi(10,[1,10]);
end
function [xB, yB] = trackB
xB = randi(10,[1,10]);
yB = randi(10,[1,10]);
end
Replace trackA and trackB function body with your code.
  1 件のコメント
Mrutyunjaya Hiremath
Mrutyunjaya Hiremath 2020 年 4 月 29 日
trackA is your trajectoryA function
function [xa,ya] = trajectoryA(VoA,ThetaA,T,g)
%Use formula (3) for xa
xa = VoA*cosd(ThetaA)*T;
%Use formula (1) for ya
ya = (VoA*sind(ThetaA)*T)-(0.5*g*T.^2)+2000;
end

サインインしてコメントする。

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by