Two conditions and not same.

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 Commenti

"From the above code value of pout and pout2 must be same"
Why must the values be same? Their definitions are different.
Herein condition 1.----if (a > lower_limit && c > 0.1 && c < 2)
condition 2 ------ if (a < lower_limit && c > 0.1 && c < 2)
also i have substracted one in pout2

Accedi per commentare.

 Risposta accettata

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
"Missing" count: 10922 "Missing" count: 10981 "Missing" count: 11058 "Missing" count: 10801 "Missing" count: 10894 "Missing" count: 10936 "Missing" count: 10988 "Missing" count: 10932 "Missing" count: 10706
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
ans = 1×9
1.0e+00 * 10922 10981 11058 10801 10894 10936 10988 10932 10706

Più risposte (0)

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Prodotti

Release

R2020a

Richiesto:

il 14 Ago 2023

Risposto:

il 14 Ago 2023

Community Treasure Hunt

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

Start Hunting!

Translated by