i have a matlab code with 3 for loops, in last for loop , there is threshold based penalization. i want trace some variables in that code and compare with plot

respected professor.
i have code with 3 for loops. 1st for delta (which gives different plots in the same figure) 2nd for eps ( which is on x axis of figure) 3rd for Nt, giving different alpha .and in the 3rd loop pout1 and pout2 is also found which is used in threshold based penalization.
i am giving the threshold based penalization problem as screenshot as well as the figure .
i have the code for generating the figure. and i am attaching the code also.
i want to trace the value of delta, eps , Nt, alpha, pout1, pout2 and get the new alpha which is in the plot and compare these values from plot.
Thank you in advance

 Accepted Answer

Make sure the variables you want to write out to the CSV file are all matrices (so you need to to create pout1 and put2 and index them in the loop).
ALso, after the loop, create Nt as a vector like this:
Nt = (0 : N)';
Look how you're creating this:
eps = 10^(sweepParam(iSweep)./10);
That's not right. eps is a built-in function and takes input arguments. You do not assign the built-in function to some number. You need to create your own indexed variable and index it, like
myEpsilon(iSweep) = 10^(sweepParam(iSweep)./10);
Then after the loop, call writematrix or writetable to write out all your variables.

1 Comment

Thanks for the eps clarity. i will change that.
you mean i have to create indexes for all and then arrange them for csv file. i will check that .
but there is simple solution i think . i came across the diary function, >>diary('C:\Users\shahn\Desktop\element splitting\output_log.txt')
to output the display in command window to text file. but i am getting wrong values in the text file. i you can help

Sign in to comment.

More Answers (1)

If I understand you correctly, you might want to try animatedline. Use this method to keep adding new points to the line as you loop, achieving the "trace" effect you want.

3 Comments

Dear professor, i tried animatedline, but was having difficulty.
however i am satisfied with the output displayed by the following code in command window. and i want to save this to csv file. with columns delta, epsilon, Nt, pout1 and pout2 . the code is :
clc;
clear all;
N = 32;
P = 10^((30 - 30)/10); P_1 = P; P_2 = P;
P_w = 10^((-110 - 30)/10);
m_h = [3, 1.5];
m_BS = 6;
for delta = [0, 5, 10, 15, 20, 25, 30]
% Adjust the path loss
ell_h = 10.^([-70, -70-delta]./10);
ell_BS = 10.^([-65]./10);
fprintf('\n=== Results for Delta = %d ===\n', delta);
% Sweep over different outage thresholds
sweepParam = [linspace(-10, 15, 13)];
for iSweep = 1:length(sweepParam)
eps = 10^(sweepParam(iSweep)./10);
fprintf('\nEpsilon (eps): %.4f\n', eps);
for Nt = 0:1:N
% Calculate N_1 and N_2
N_1 = Nt;
N_2 = N - N_1;
% Initialize variables
k = zeros(2,1);
theta = zeros(2,1);
% UE1
iUE = 1;
mu_1 = gamma(m_BS + 1/2) * gamma(m_h(iUE) + 1/2) / ...
(gamma(m_BS) * gamma(m_h(iUE)) * (m_BS * m_h(iUE))^(1/2));
kS_1 = N_1 * mu_1^2 / (1 - mu_1^2);
theta_S1 = (1 - mu_1^2) / mu_1;
mu_H1c = exp(gammaln(kS_1 + (1:4)) + (1:4) .* log(theta_S1) - gammaln(kS_1));
mu_H1r = exp(gammaln(1 + (1:4)./2) + ((1:4)./2) .* log(N_2));
[k(iUE), theta(iUE)] = matchMoments(mu_H1c(2) + mu_H1r(2), ...
mu_H1c(4) + mu_H1r(4) + 4*mu_H1c(2)*mu_H1r(2));
% UE2
iUE = 2;
mu_2 = gamma(m_BS + 1/2) * gamma(m_h(iUE) + 1/2) / ...
(gamma(m_BS) * gamma(m_h(iUE)) * (m_BS * m_h(iUE))^(1/2));
kS_1 = N_2 * mu_2^2 / (1 - mu_2^2);
theta_S1 = (1 - mu_2^2) / mu_2;
mu_H2c = exp(gammaln(kS_1 + (1:4)) + (1:4) .* log(theta_S1) - gammaln(kS_1));
mu_H2r = exp(gammaln(1 + (1:4)./2) + ((1:4)./2) .* log(N_1));
[k(iUE), theta(iUE)] = matchMoments(mu_H2c(2) + mu_H2r(2), ...
mu_H2c(4) + mu_H2r(4) + 4*mu_H2c(2)*mu_H2r(2));
theta(1) = theta(1) * ell_BS * ell_h(1);
theta(2) = theta(2) * ell_BS * ell_h(2);
% Outage probability calculation
% UE1
P_i = P_1; P_j = P_2;
k_i = k(1);
theta_i = theta(1) * P_i;
k_j = (k(2) * theta(2) * P_j + P_w)^2 / (k(2) * (theta(2) * P_j)^2);
theta_j = k(2) * (theta(2) * P_j)^2 / (k(2) * theta(2) * P_j + P_w);
p_succ1 = 1 - betainc(eps / (eps + theta_i / theta_j), k_i, k_j);
p_succSNR1 = 1 - gamcdf(eps * P_w, k_i, theta_i);
% UE2
P_i = P_2; P_j = P_1;
k_i = k(2);
theta_i = theta(2) * P_i;
k_j = (k(1) * theta(1) * P_j + P_w)^2 / (k(1) * (theta(1) * P_j)^2);
theta_j = k(1) * (theta(1) * P_j)^2 / (k(1) * theta(1) * P_j + P_w);
p_succ2 = 1 - betainc(eps / (eps + theta_i / theta_j), k_i, k_j);
p_succSNR2 = 1 - gamcdf(eps * P_w, k_i, theta_i);
pout = [1 - min(p_succ1 + p_succ2 * p_succSNR1, p_succSNR1), ...
1 - min(p_succ2 + p_succ1 * p_succSNR2, p_succSNR2)];
% Output results for each Nt
fprintf('Nt = %2d -> Pout1: %.6f, Pout2: %.6f\n', Nt, pout(1), pout(2));
end
fprintf('---------------------\n'); % Separate each epsilon value
end
fprintf('=====================\n'); % Separate each delta value
end
Exactly what variables do you want to write out? Make sure the variables you want to write out to the CSV file are all matrices (so you need to index them in the loop), and then after the loop, call writematrix or writetable.
Writing to CSV is a different question. Do you have difficulty in constructing data structure suitable for CSV or the usage of APIs related to CSV writing?

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2024b

Community Treasure Hunt

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

Start Hunting!