Can anyone help me to rectify this matlab code? i

clc; clear variables; close all;
N = 10.^6;
d1 = 0.3; %Source to user 1 distance
d2 = 1; %Source to user 2 distance
d12 = 1-d1; %User 1 to user 2 distance
alpha=2;
Omega_1=d1.^(-alpha);
Omega_2=d2.^(-alpha);
Omega_12=d12.^(-alpha);
m = 1;
%h1=sqrt(gamrnd (m,Omega_1./m,1,N));
%h2=sqrt(gamrnd (m,Omega_2./m,1,N));
%h12=sqrt(gamrnd (m,Omega_12./m,1,N));
%g1 = (abs(h1)).^2;
%g2 = (abs(h2)).^2;
%g12 = (abs(h12)).^2;
%gLI=10.^(-1.5);
SNR = -10:5:50; %SNR in dB
snr = db2pow(SNR); %SNR linear
a1 = 0.2; a2 = 0.8; %Power allocation coefficients
R2 = 0.5; %Target rate
R1=3;
gamma_th1_FD=2.^R1-1;
gamma_th2_FD=2.^R2-1;
tau_1=gamma_th2_FD./(snr.*(a2-a1.*gamma_th2_FD));
beta_1=gamma_th1_FD./(a1.*snr);
theta_1=max(tau_1,beta_1);
gamma_th1_HD=2.^(2.*R1)-1;
gamma_th2_HD=2.^(2.*R2)-1;
tau_2=gamma_th2_HD./(snr.*(a2-a1.*gamma_th2_HD));
beta_2=gamma_th1_HD./(a1.*snr);
theta_2=max(tau_2,beta_2);
y= zeros(m,1);
for i=0:m-1
k=i+1;
y=(1./factorial(k)).*(theta_2.^k).*(m./Omega_1).^k;
end
y1=sum(y);
y2=exp(-(m.*theta_2./Omega_1)).*y1;
p=1-y2;
plot(SNR,p,'--dc','linewidth',2);hold on;grid on;

11 Comments

What exactly is the problem? The code runs without error. If it's not doing something you expect, you'll have to explain.
actually i want to plot an equation (explained in attached file) in matlab, but it is showing me some errors. you can check an equation in attached file.
u can understand from the code the values of tau_2,beta_2 and theta_2 that are in the equation.
clc; clear variables; close all;
N = 10.^6;
d1 = 0.3; %Source to user 1 distance
d2 = 1; %Source to user 2 distance
d12 = 1-d1; %User 1 to user 2 distance
alpha=2;
Omega_1=d1.^(-alpha);
Omega_2=d2.^(-alpha);
Omega_12=d12.^(-alpha);
m = 1;
%h1=sqrt(gamrnd (m,Omega_1./m,1,N));
%h2=sqrt(gamrnd (m,Omega_2./m,1,N));
%h12=sqrt(gamrnd (m,Omega_12./m,1,N));
%g1 = (abs(h1)).^2;
%g2 = (abs(h2)).^2;
%g12 = (abs(h12)).^2;
%gLI=10.^(-1.5);
SNR = -10:5:50; %SNR in dB
snr = db2pow(SNR); %SNR linear
a1 = 0.2; a2 = 0.8; %Power allocation coefficients
R2 = 0.5; %Target rate
R1=3;
gamma_th1_FD=2.^R1-1;
gamma_th2_FD=2.^R2-1;
tau_1=gamma_th2_FD./(snr.*(a2-a1.*gamma_th2_FD));
beta_1=gamma_th1_FD./(a1.*snr);
theta_1=max(tau_1,beta_1);
gamma_th1_HD=2.^(2.*R1)-1;
gamma_th2_HD=2.^(2.*R2)-1;
tau_2=gamma_th2_HD./(snr.*(a2-a1.*gamma_th2_HD));
beta_2=gamma_th1_HD./(a1.*snr);
theta_2=max(tau_2,beta_2);
y= zeros(m,1);
for i=0:m-1
k=i+1;
y=(1./factorial(k)).*(theta_2.^k).*(m./Omega_1).^k;
end
y1=sum(y);
y2=exp(-(m.*theta_2./Omega_1)).*y1;
p=1-y2;
plot(SNR,p,'--dc','linewidth',2);hold on;grid on;
The above plot is shown up on running the give code. What errors you are getting? Specify the errors and line number.
this code is shoeing errors:
clc; clear variables; close all;
N = 10.^6;
d1 = 0.3; %Source to user 1 distance
d2 = 1; %Source to user 2 distance
d12 = 1-d1; %User 1 to user 2 distance
alpha=2;
Omega_1=d1.^(-alpha);
Omega_2=d2.^(-alpha);
Omega_12=d12.^(-alpha);
m = 1;
%h1=sqrt(gamrnd (m,Omega_1./m,1,N));
%h2=sqrt(gamrnd (m,Omega_2./m,1,N));
%h12=sqrt(gamrnd (m,Omega_12./m,1,N));
%g1 = (abs(h1)).^2;
%g2 = (abs(h2)).^2;
%g12 = (abs(h12)).^2;
%gLI=10.^(-1.5);
SNR = -10:5:50; %SNR in dB
snr = db2pow(SNR); %SNR linear
a1 = 0.2; a2 = 0.8; %Power allocation coefficients
R2 = 0.5; %Target rate
R1=3;
gamma_th1_FD=2.^R1-1;
gamma_th2_FD=2.^R2-1;
tau_1=gamma_th2_FD./(snr.*(a2-a1.*gamma_th2_FD));
beta_1=gamma_th1_FD./(a1.*snr);
theta_1=max(tau_1,beta_1);
gamma_th1_HD=2.^(2.*R1)-1;
gamma_th2_HD=2.^(2.*R2)-1;
tau_2=gamma_th2_HD./(snr.*(a2-a1.*gamma_th2_HD));
beta_2=gamma_th1_HD./(a1.*snr);
theta_2=max(tau_2,beta_2);
%m=ones(1,length(snr));
y= zeros(m,1);
for i=0:m-1
k=i+1;
y(k)=(1./factorial(k)).*(theta_2.^k).*(m./Omega_1).^k;
end
y1=sum(y);
y2=exp(-(m.*theta_2./Omega_1)).*y1;
p=1-y2;
plot(SNR,p,'--dc','linewidth',2);hold on;grid on;
Please include the exact error message.
What errors are you getting? I don't get any errors when I run it.
when running the code with y(k), it shows me an error like
Unable to perform assignment because the left and right sides have a different number of elements.
theta_2 seems to be an array, and you can't assign an array to the single scalar element y(1).
Check size(theta_2).
yes that is the problem ,how can i remove this error?
Maybe you can use y(:,k) instead of y(k).
But since I don't know what you are trying to do, you are the only person who knows how to change the code adequately.

Sign in to comment.

Answers (1)

clc; clear variables; close all;
N = 10.^6;
d1 = 0.3; %Source to user 1 distance
d2 = 1; %Source to user 2 distance
d12 = 1-d1; %User 1 to user 2 distance
alpha=2;
Omega_1=d1.^(-alpha);
Omega_2=d2.^(-alpha);
Omega_12=d12.^(-alpha);
m = 1;
%h1=sqrt(gamrnd (m,Omega_1./m,1,N));
%h2=sqrt(gamrnd (m,Omega_2./m,1,N));
%h12=sqrt(gamrnd (m,Omega_12./m,1,N));
%g1 = (abs(h1)).^2;
%g2 = (abs(h2)).^2;
%g12 = (abs(h12)).^2;
%gLI=10.^(-1.5);
SNR = -10:5:50; %SNR in dB
snr = db2pow(SNR); %SNR linear
a1 = 0.2; a2 = 0.8; %Power allocation coefficients
R2 = 0.5; %Target rate
R1=3;
gamma_th1_FD=2.^R1-1;
gamma_th2_FD=2.^R2-1;
tau_1=gamma_th2_FD./(snr.*(a2-a1.*gamma_th2_FD));
beta_1=gamma_th1_FD./(a1.*snr);
theta_1=max(tau_1,beta_1);
gamma_th1_HD=2.^(2.*R1)-1;
gamma_th2_HD=2.^(2.*R2)-1;
tau_2=gamma_th2_HD./(snr.*(a2-a1.*gamma_th2_HD));
beta_2=gamma_th1_HD./(a1.*snr);
theta_2=max(tau_2,beta_2);
%m=ones(1,length(snr));
y= cell(m,1);
for i=0:m-1
k=i+1;
y{k}=(1./factorial(k)).*(theta_2.^k).*(m./Omega_1).^k;
end
y = cell2mat(y) ;
y1=sum(y);
y2=exp(-(m.*theta_2./Omega_1)).*y1;
p=1-y2;
plot(SNR,p,'--dc','linewidth',2);hold on;grid on;

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 19 Jul 2021

Answered:

on 19 Jul 2021

Community Treasure Hunt

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

Start Hunting!