フィルターのクリア

How to plot two line in one figure?

1 回表示 (過去 30 日間)
Mussab Arab
Mussab Arab 2018 年 6 月 13 日
コメント済み: Mussab Arab 2018 年 6 月 14 日
I'm working on this code and I want to plot the result for two values of XNP for example:
XNP=4 and 5
I tried to use (hold on) but it keeps giving me one line
n = 0;
VM = 881;
VT = 600;
XNT = 1;
HEDEG = -20;
XNP = 4;
RM1 = 0;
RM2 = 0;
RT1 = 40000;
RT2 = 10000;
BETA = 0.85;
VT1 = -VT*cos(BETA);
VT2 = VT*sin(BETA);
HE = HEDEG/57.3;
T = 0;
S = 0;
RTM1 = RT1 - RM1;
RTM2 = RT2 - RM2;
RTM = sqrt(RTM1*RTM1+RTM2*RTM2);
XLAM = atan2(RTM2,RTM1);
XLEAD = asin(VT*sin(BETA+XLAM)/VM);
THET = XLAM + XLEAD ;
VM1 = VM*cos(THET+HE);
VM2 = VM*sin(THET+HE);
VTM1 = VT1 - VM1;
VTM2 = VT2 - VM2;
VC = -(RTM1*VTM1+RTM2*VTM2)/RTM;
while VC >= 0
if RTM < 1000
H = 0.0002;
else
H = 0.01;
end
BETAOLD = BETA;
RT1OLD = RT1;
RT2OLD =RT2;
RM1OLD = RM1;
RM2OLD = RM2;
VM1OLD = VM1;
VM2OLD = VM2;
STEP = 1;
FLAG = 0;
while STEP <= 1
if FLAG == 1
STEP = 2;
BETA = BETA+H*BETAD;
RT1 = RT1 +H*VT1;
RT2 = RT2 +H*VT2;
RM1 = RM1 +H*VM1;
RM2 = RM2 +H*VM2;
VM1 = VM1 +H*AM1;
VM2 = VM2 +H*AM2;
T=T+H;
end
RTM1=RT1-RM1;
RTM2=RT2-RM2;
RTM=sqrt(RTM1*RTM1+RTM2*RTM2);
VTM1=VT1-VM1;
VTM2=VT2-VM2;
VC=-(RTM1*VTM1+RTM2*VTM2)/RTM;
XLAM=atan2(RTM2,RTM1);
XLAMD=(RTM1*VTM2-RTM2*VTM1)/(RTM*RTM);
XNC=XNP*VC*XLAMD;
AM1=-XNC*sin(XLAM);
AM2=XNC*cos(XLAM);
VT1=-VT*cos(BETA);
VT2=VT*sin(BETA);
BETAD=XNT/VT;
FLAG=1;
end
FLAG=0;
BETA=.5*(BETAOLD+BETA+H*BETAD);
RT1=.5*(RT1OLD+RT1+H*VT1);
RT2=.5*(RT2OLD+RT2+H*VT2);
RM1=.5*(RM1OLD+RM1+H*VM1);
RM2=.5*(RM2OLD+RM2+H*VM2);
VM1=.5*(VM1OLD+VM1+H*AM1);
VM2=.5*(VM2OLD+VM2+H*AM2);
S=S+H;
if S >=.09999
S=0.;
n=n+1;
ArrayT(n)=T;
ArrayRT1(n)=RT1;
ArrayRT2(n)=RT2;
ArrayRM1(n)=RM1;
ArrayRM2(n)=RM2;
ArrayXNCG(n)=XNC/32.2;
ArrayRTM(n)=RTM;
end
end
RTM
figure
plot(ArrayRT1,ArrayRT2,ArrayRM1,ArrayRM2),grid
title('Two-dimensional tactical missile-target engagement simulation')
xlabel('Downrange (Ft) ')
ylabel('Altitude (Ft)')
figure
plot(ArrayT,ArrayXNCG),grid
title('Two-dimensional tactical missile-target engagement simulation')
xlabel('Time (sec)')
ylabel('Acceleration of missle (G)')
clc
output=[ArrayT',ArrayRT1',ArrayRT2',ArrayRM1',ArrayRM2',ArrayXNCG',ArrayRTM' ];
save datfil.txt output
disp '*** Simulation Complete'

採用された回答

Tejas Jayashankar
Tejas Jayashankar 2018 年 6 月 14 日
Hi,
If you want the plots for different values of XNP to appear on the same graph, instead of using figure, number your figures as figure(1) and figure(2). You were correct when you were using hold on and hold off. By numbering your figure, when you change the value of XNP to 5 from 4, the new line will be overlap the plot generated when XNP = 4. Make sure that you do not close the plots generated when XNP = 4. A better method would be to modify your code so that XNP is a vector of values.
I have pasted the modified code below. Look at the comments at the bottom of the code to see the changes.
n = 0;
VM = 881;
VT = 600;
XNT = 1;
HEDEG = -20;
XNP = 5;
RM1 = 0;
RM2 = 0;
RT1 = 40000;
RT2 = 10000;
BETA = 0.85;
VT1 = -VT*cos(BETA);
VT2 = VT*sin(BETA);
HE = HEDEG/57.3;
T = 0;
S = 0;
RTM1 = RT1 - RM1;
RTM2 = RT2 - RM2;
RTM = sqrt(RTM1*RTM1+RTM2*RTM2);
XLAM = atan2(RTM2,RTM1);
XLEAD = asin(VT*sin(BETA+XLAM)/VM);
THET = XLAM + XLEAD ;
VM1 = VM*cos(THET+HE);
VM2 = VM*sin(THET+HE);
VTM1 = VT1 - VM1;
VTM2 = VT2 - VM2;
VC = -(RTM1*VTM1+RTM2*VTM2)/RTM;
while VC >= 0
if RTM < 1000
H = 0.0002;
else
H = 0.01;
end
BETAOLD = BETA;
RT1OLD = RT1;
RT2OLD =RT2;
RM1OLD = RM1;
RM2OLD = RM2;
VM1OLD = VM1;
VM2OLD = VM2;
STEP = 1;
FLAG = 0;
while STEP <= 1
if FLAG == 1
STEP = 2;
BETA = BETA+H*BETAD;
RT1 = RT1 +H*VT1;
RT2 = RT2 +H*VT2;
RM1 = RM1 +H*VM1;
RM2 = RM2 +H*VM2;
VM1 = VM1 +H*AM1;
VM2 = VM2 +H*AM2;
T=T+H;
end
RTM1=RT1-RM1;
RTM2=RT2-RM2;
RTM=sqrt(RTM1*RTM1+RTM2*RTM2);
VTM1=VT1-VM1;
VTM2=VT2-VM2;
VC=-(RTM1*VTM1+RTM2*VTM2)/RTM;
XLAM=atan2(RTM2,RTM1);
XLAMD=(RTM1*VTM2-RTM2*VTM1)/(RTM*RTM);
XNC=XNP*VC*XLAMD;
AM1=-XNC*sin(XLAM);
AM2=XNC*cos(XLAM);
VT1=-VT*cos(BETA);
VT2=VT*sin(BETA);
BETAD=XNT/VT;
FLAG=1;
end
FLAG=0;
BETA=.5*(BETAOLD+BETA+H*BETAD);
RT1=.5*(RT1OLD+RT1+H*VT1);
RT2=.5*(RT2OLD+RT2+H*VT2);
RM1=.5*(RM1OLD+RM1+H*VM1);
RM2=.5*(RM2OLD+RM2+H*VM2);
VM1=.5*(VM1OLD+VM1+H*AM1);
VM2=.5*(VM2OLD+VM2+H*AM2);
S=S+H;
if S >=.09999
S=0.;
n=n+1;
ArrayT(n)=T;
ArrayRT1(n)=RT1;
ArrayRT2(n)=RT2;
ArrayRM1(n)=RM1;
ArrayRM2(n)=RM2;
ArrayXNCG(n)=XNC/32.2;
ArrayRTM(n)=RTM;
end
end
RTM
hold on % Added to plot multiple lines
figure(1); % Number your figure
plot(ArrayRT1,ArrayRT2,ArrayRM1,ArrayRM2),grid ON % It is better to use grid ON
title('Two-dimensional tactical missile-target engagement simulation')
xlabel('Downrange (Ft) ')
ylabel('Altitude (Ft)')
hold off
hold on
figure(2); % Number your figure
plot(ArrayT,ArrayXNCG),grid ON
title('Two-dimensional tactical missile-target engagement simulation')
xlabel('Time (sec)')
ylabel('Acceleration of missle (G)')
hold off
clc
output=[ArrayT',ArrayRT1',ArrayRT2',ArrayRM1',ArrayRM2',ArrayXNCG',ArrayRTM' ];
save datfil.txt output
disp '*** Simulation Complete'
  1 件のコメント
Mussab Arab
Mussab Arab 2018 年 6 月 14 日
Thanks a lot you have helped me a lot
but if you can help me with modifying the code so that XNP is a vector of values.
because I have tried and searched but I couldn't find anything.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by