MATLAB: Problem with legend in two for loops

legend in two for loops

hello all, I use two for- loop to do my exercises, but when I run, It is only display a legend and it's the wrong way. In my code, if I use the legend ('show'), It will display 6 lines in the legend in black and red, but I only wold like to display two lines for two for – loops, corresponding, one line for for-loop above(in code) and one line of legend for for-loop at bottom (for-loop in code) with the color like in my code. So how can i do. So, how do I edit the code? I attach the picture that i would to do legend the same it ( in my code, It show the same 6 line, but I don't kown how to make the legend like it)
Please help! Code in attach file

Best Answer

clc; clear all ;
clear all;
clf;
clc;
alpha = 4;
P1t = 2/3;
P2t = 2/3;
Pct = 2/3;
d12 = 1;
d1r = 0.6;
d2r = 0.4;
Ptra1t = 1;
Ptra2t = Ptra1t;
Ptra12 = Ptra1t*d12^-alpha;
Ptra21 = Ptra12;
P12 = P1t*1^-alpha;
P21 = P12;
P1r = P1t*d1r^-alpha;
P2r = P2t*d2r^-alpha;
Pdr = 2*P1r;
Pr1 = P1r;
Pr2 = P2r;
Prd = Pr1;
Pcr = Pct*5^-alpha;
Pc1 = Pct*6^-alpha;
Yha = 0:2:4;
Yh = 10.^(Yha/10);
Pb1 = (1/20)*P1t;
Pb2 = Pb1;
Pbd = Pb1;
Pbr = Pb1;
NRa = -5:25;
NR = 10.^(NRa/10);
N0=P12./NR;
P = zeros(3,length(NRa)) ;
for i = 1:3
Po1 = (1/((Pcr/Pdr)*Yh(i)+1))^2 * (1/((Pbr/Pdr)*Yh(i)+1))^2 .* exp(-(2*P12./(Pdr*NR) + 2*P12./(Prd*NR))*Yh(i));
Po21 = (2-(P12./(Pcr*NR))).*exp(-(P12./(Pdr*NR))*(Yh(i)^2 + 2*Yh(i))) / ((Pcr/Pdr)*(Yh(i)^2 + 2*Yh(i))+1);
Po22 = (P12./(Pcr*NR)).*exp(-(P12./(Pdr*NR))*(Yh(i)^2 + 2*Yh(i))) - exp(-(P12./(Pdr*NR))*(Yh(i)^2 + 2*Yh(i)))/((Pcr/Pdr)*(Yh(i)^2 + 2*Yh(i))+1)^2;
Po2 = Po21 + Po22;
P(i,:) = 1 - Po1 .* Po2;
end
Pout = zeros(3,length(NRa)) ;
for i = 1:3
Pa=Ptra12./((Pc1.*Yh(i))+Ptra12);
Pb=(Ptra21./((Pb1.*Yh(i))+Ptra21))*exp(-(P12./(NR*Ptra12)).*Yh(i)-(P12./(NR*Ptra21)).*Yh(i));
Pout(i,:)=1-Pa.*Pb;
% text(0.1,0.13,{'theta=0,2,4'},...
% 'vertic','middle','horiz','center','color','k','FontSize',12);
end
NRa = repmat(NRa,[3 1]) ;
NRa(:,end+1) = NaN ;NRa = NRa' ; NRa = NRa(:) ;
P(:,end+1) = NaN ;P = P' ; P = P(:) ;
Pout(:,end+1) = NaN ;Pout = Pout' ; Pout = Pout(:) ;
semilogy(NRa,P,'-.r');
hold on
semilogy(NRa,Pout,'k');
legend ({'aided case1' , 'aided case2'});
grid on;