Two conditions and not same.
2 views (last 30 days)
Show older comments
clear all;
clc;
close all;
sigma = 1;
snrdb = 0:5:40;
p =10^4* 10.^(snrdb/10);
eta = 0.5;
u = 0.55;
yth = 0.1;
alp = 0.4;
R = 1;
h = 2;
eb = 0.1;
m = 2;
T = 1;
pth = 2;
for i= 1:length(snrdb)
j=0;k=0;
M=100000;
A = (yth * sigma) ./ p(i);
B = pth ./ p(i);
C = (yth * T * sigma * (1 - alp)) / (eta * alp * T * p(i) * 2 * (1 - u));
D = eb / (eta * alp * T * p(i));
for N= 1:1:M
c=gamrnd(2,0.5,1);
d2=2;
a=gamrnd(1,1,1);
d1=gamrnd(1,1,1);
snr=a/d1^2;
snr1 = (C .* d2.^2) / (B + D);
snr2 = (C .* d2.^2) / (A + D);
lower_limit = ((C * d2^2 - D * c) * d1^2) / c;
upper_limit = (pth * d1^2) / p(i);
if (a > lower_limit && c > 0.1 && c < 2)
j=j+1;
else
j=j;
end
if (a < lower_limit && c > 0.1 && c < 2)
k=k+1;
else
k=k;
end
end
pout(i)=(j/M);
pout1(i)=k/M;
pout2(i)=1-pout1(i);
end
semilogy(snrdb,pout,'-y','LineWidth',0.6,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',6)
hold on
semilogy(snrdb,pout2,'--r','LineWidth',0.6,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',6)
From the above code value of pout and pout2 must be same, but i am getting different values. PLEASE resolve this.
2 Comments
Dyuman Joshi
on 14 Aug 2023
"From the above code value of pout and pout2 must be same"
Why must the values be same? Their definitions are different.
Accepted Answer
Voss
on 14 Aug 2023
pout(i)=(j/M);
pout1(i)=k/M;
pout2(i)=1-pout1(i);
So pout2(i) is 1-(k/M), which is (M-k)/M, right?
So in saying that pout and pout2 must be the same, apparently you believe that M-k must equal j. Do you imagine that the sum of j and k will always be M because the two if conditions are opposite of each other, so that if one is true the other must be false (and vice versa)? In other words, do you imagine that exactly one of j and k (but not both) will be incremented each time through the loop? If that's what you are thinking, it is not the case.
Consider what happens if a == lower_limit or c <= 0.1 or c >= 2. In those situations, neither if condition is true, so neither j nor k is incremented.
Here is your code, with some logic added to keep track of how many times both if conditions are false. You'll see below that it happens about 11% of the time, and it exactly explains the gap you see between the lines.
clear all;
clc;
close all;
sigma = 1;
snrdb = 0:5:40;
p =10^4* 10.^(snrdb/10);
eta = 0.5;
u = 0.55;
yth = 0.1;
alp = 0.4;
R = 1;
h = 2;
eb = 0.1;
m = 2;
T = 1;
pth = 2;
for i= 1:length(snrdb)
j=0;k=0;
M=100000;
A = (yth * sigma) ./ p(i);
B = pth ./ p(i);
C = (yth * T * sigma * (1 - alp)) / (eta * alp * T * p(i) * 2 * (1 - u));
D = eb / (eta * alp * T * p(i));
missing_counts(i) = 0;
for N= 1:1:M
c=gamrnd(2,0.5,1);
d2=2;
a=gamrnd(1,1,1);
d1=gamrnd(1,1,1);
snr=a/d1^2;
snr1 = (C .* d2.^2) / (B + D);
snr2 = (C .* d2.^2) / (A + D);
lower_limit = ((C * d2^2 - D * c) * d1^2) / c;
upper_limit = (pth * d1^2) / p(i);
if (a > lower_limit && c > 0.1 && c < 2)
incremented_j = true;
j=j+1;
else
j=j;
incremented_j = false;
end
if (a < lower_limit && c > 0.1 && c < 2)
incremented_k = true;
k=k+1;
else
k=k;
incremented_k = false;
end
% neither j nor k incremented this time -> add 1 to missing_counts(i)
if ~incremented_j && ~incremented_k
missing_counts(i) = missing_counts(i)+1;
end
end
pout(i)=(j/M);
pout1(i)=k/M;
pout2(i)=1-pout1(i);
% Note: missing_counts(i) == M-(j+k)
fprintf('"Missing" count: %d\n',missing_counts(i));
end
semilogy(snrdb,pout,'-y','LineWidth',0.6,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',6)
hold on
semilogy(snrdb,pout2,'--r','LineWidth',0.6,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',6)
The difference between pout2 and pout (scaled up by M) matches those "missing" counts exactly:
format short g
(pout2-pout)*M
0 Comments
More Answers (0)
See Also
Categories
Find more on Testing Frameworks 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!