You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
why do I always receive the error: Unrecognized function or variable 'Pt'.
2 views (last 30 days)
Show older comments
function [Cout,iter_time] = PGM_opt(Pt,Hdir,H1,H2,maxIter,Qinit,myomegaunitball,c)
17 Comments
Dyuman Joshi
on 5 Jan 2023
Edited: Dyuman Joshi
on 5 Jan 2023
Please show your full code, how you call the function and the full error.
LoCi
on 5 Jan 2023
Edited: Voss
on 6 Jan 2023
% Function that implements the PGM iterative optimization
function [Cout,iter_time] = PGM_opt(Pt,Hdir,H1,H2,maxIter,Qinit,myomegaunitball,c)
% Load the initial covariance matrix and RIS phase shifts
myomegaunitcirc = myomegaunitball.';
Q = Qinit;
% Line-search parameters
delta = 1e-5;
rho = 0.5;
iIter = 0;
stepsize = 10000; % Inital step size
Cout = [RIScap(Hdir,H2,H1,myomegaunitcirc,Q)]; % Initial achievable rate
Cprev = Cout;
iter_time = 0;
tic
while iIter<maxIter
iIter = iIter+1;
q_Q = gracov(Hdir,H2,H1,myomegaunitcirc,Q); % gradient w.r.t. Q
g_RIS = gradRIS(Hdir,H2,H1,myomegaunitcirc,Q); % gradient w.r.t. RIS
for iLineSearch = 0:30
% Updated covariance (Q) matrix, line 3 of Algorithm 1
Qnew = Q + stepsize*q_Q;
Qnew = cov_mat_proj_modified(Qnew,Pt*c^2);
% Updated RIS phase shifts, line 4 of Algorithm 1
yunitcirc = myomegaunitcirc + stepsize*g_RIS;
myomegaunitcircnext = projectontounitcircle(yunitcirc)/c;
% New achievable rate
Cnew = RIScap(Hdir,H2,H1,myomegaunitcircnext,Qnew);
% Line-search procedure
if (((Cnew-Cprev) >= delta*(norm(Qnew-Q)^2+ ...
norm(myomegaunitcircnext-myomegaunitcirc)^2)) ...
|| (stepsize<1e-4) )
% If the step size satisfies (35c) OR it is too small:
% Obtained Q matrix and RIS phase shifts
myomegaunitcirc = myomegaunitcircnext;
Q = Qnew;
Cprev = Cnew;
break
else
% Reduce the step size
stepsize=stepsize*rho;
end
end
% New achievable rate
Cout = [Cout Cnew];
% Exection time of an iteration
iter_time = [iter_time toc];
end
end
% Calculation of gradient w.r.t. Q
function y = gracov(Hdir,H2,H1,myomega,Q)
Z = Hdir+H2*diag(myomega)*H1;
Nr = size(H2,1);
y = Z'*inv(eye(Nr)+Z*Q*Z')*Z;
end
% Calculation of gradient w.r.t. RIS
function y = gradRIS(Hdir,H2,H1,myomega,Q)
Z = Hdir+H2*diag(myomega)*H1;
Nr = size(H2,1);
y = diag(H2'*inv(eye(Nr)+Z*Q*Z')*Z*Q*H1');
end
% Achievable rate calculation
function y = RIScap(Hdir,H2,H1,myomega,Q)
Z = Hdir+H2*diag(myomega)*H1;
Nr = size(H2,1);
y = real(log(det(eye(Nr)+Z*Q*Z')))/log(2);
end
% RIS projection
function y = projectontounitcircle(x)
y = x./abs(x);
end
% Covarinace matrix projection
function Qnew = cov_mat_proj_modified(Qold,Pt)
[U,D] = eig(Qold);
Dnew = water_fill(Pt,real(diag(D))).';
Qnew = U*diag(Dnew)*U';
end
% Water-filling algorithm
function vect_out = water_fill(Pt,vect_in)
vect_in = vect_in.';
[sort_val,sort_idx] = sort(vect_in,'descend');
for n = length(vect_in):-1:1
water_level = (sum(sort_val(1:n))-Pt)/n;
di = sort_val(1:n)-water_level;
if di(:)>=0
break
end
end
vect_out = zeros(1,length(vect_in));
vect_out(sort_idx(1:n)) = di;
end
Dyuman Joshi
on 5 Jan 2023
You still haven't attached how you call the function and what the full error is.
LoCi
on 6 Jan 2023
Edited: Voss
on 6 Jan 2023
clear; close all; clc;
Nt = 8; % Number of TX antennas
Nr = 4; % Number of RX antennas
Nris = 15^2; % Number of RIS elements
K = 1; % Rician factor
D = 500; % TX-RX distance
dist_ris = 40; % RIS distance from TX
f = 2e9; % Frequency
lt = 20; % TX position
lr = 100; % RX position
Pt = 1; % Transmit power in Watts
N0 = -120; % Noise power in dB
SNR = db2pow(-N0); % SNR
no_mat = 10; % Number of channel realizations
no_iter = 500; % Number of iterations
alpha_dir = 3; % FSPL exponent of the direct link
% Generate channel matrices (WORNING: It is only works for Nris that is a square number)
[Hdirt,H1t,H2t] = chan_mat_RIS_surf_univ_new(Nt,Nr,Nris,lt,lr,D,no_mat,K,f,dist_ris,alpha_dir);
Cpgm = zeros(1,no_iter+1);
for i = 1:no_mat
Hdir = Hdirt{i}; H1 = H1t{i}; H2 = H2t{i};
% Scaling factor
c = sqrt(norm(Hdir)/norm(H2*H1))*max(sqrt(Pt),1)/sqrt(Pt)*10;
% Initial Q matrix and RIS phase shifts
Qinit = eye(Nt)*(Pt/Nt);
omega_init = ones(1,Nris);
% PGM iterative optimization
[dCpgm,~] = PGM_opt(Pt,Hdir*sqrt(SNR)/c,H1*sqrt(SNR),H2,no_iter,Qinit*c^2,omega_init/c,c);
Cpgm = Cpgm+dCpgm;
end
semilogx(1:length(Cpgm),Cpgm/no_mat,'r','DisplayName','PGM');
xlabel('Iteration number'); ylabel('Achievable rate [bit/s/Hz]');
xlim([0 no_iter]);
legend('show','Location','SouthEast');
print('../results/Achievable_Rate', '-dpdf')
LoCi
on 6 Jan 2023
Edited: Voss
on 6 Jan 2023
function [Hdir,H1,H2] = chan_mat_RIS_surf_univ_new(Nt,Nr,Nris,lt,lr,D,no_mat,K,f,dist_ris,varargin)
lambda = 3e8/f; % Wavelength
dt = lambda/2; % TX antenna space
dr = lambda/2; % RX antenna space
dris = lambda/2; % RIS element space
k = 2*pi/lambda; % Wavenumber
% Geometrical placement
% x, y and z axis
% TX antenna array
tx_arr(1,:) = zeros(1,Nt);
tx_arr(2,:) = (sort(0:Nt-1,'descend')-(Nt-1)/2)*dt+lt;
tx_arr(3,:) = zeros(1,Nt);
% RX antenna array
rx_arr(1,:) = D*ones(1,Nr);
rx_arr(2,:) = (sort(0:Nr-1,'descend')-(Nr-1)/2)*dr+lr;
rx_arr(3,:) = zeros(1,Nr);
% RIS
center = [dist_ris 0]; % RIS center position
N1 = sqrt(Nris);
N2 = N1; % Number of RIS elements in two dimensions N1 and N2
ris_pos = RISPosition(N1,N2,dris,center); % RIS elements' coordinates
a = repmat(ris_pos{1},N1,1); % Placing RIS elements in proper coordinates
ris_arr(1,:) = a(:)';
ris_arr(2,:) = zeros(1,Nris);
ris_arr(3,:) = repmat(ris_pos{2},1,N2);
if isempty(varargin) % Load the FSPL of the direct link
alpha = 2;
else
alpha = varargin{1};
end
% direct TX-RX paths/channel matrix
for i1 = 1:Nr % Distance between the TX and RX antennas
for j1 = 1:Nt
d(i1,j1) = norm(rx_arr(:,i1)-tx_arr(:,j1));
end
end
Hdir_los = exp(-1i*k*d); % Direct link, LOS matrix exponents
tx_rx_dist = sqrt(D^2+(lt-lr)^2); % TX-RX distance
FSPL_dir = (lambda/(4*pi))^2/tx_rx_dist^alpha(1); % Inversion of the FSPL of the direct link
Hdir = Rician(Hdir_los,sqrt(FSPL_dir),no_mat,K); % Direct link channel matrix
% indirect paths (TX-RIS-RX)
for l1 = 1:Nris % Distance between the RIS elements and the RX antennas
for r1 = 1:Nr
d2(r1,l1) = norm(rx_arr(:,r1)-ris_arr(:,l1));
end
for t1 = 1:Nt % Distance between the RIS elements and the TX antennas
d1(l1,t1) = norm(tx_arr(:,t1)-ris_arr(:,l1));
end
end
tx_ris_dist = sqrt(dist_ris^2+lt^2); % TX-RIS distance
ris_rx_dist = sqrt((D-dist_ris)^2+lr^2); % RIS-RX distance
FSPLindir = lambda^4/(256*pi^2)*... % Inversion of the FSPL of the indirect link
((lt/tx_ris_dist+lr/ris_rx_dist)^2)*...
1/(tx_ris_dist*ris_rx_dist)^2;
% TX-RIS channel matrix
H1_los = exp(-1i*k*d1); % TX-RIS link, LOS matrix exponents
FSPL_1 = sqrt(FSPLindir); % FSPL of the indirect link is embedded in the TX-RIS channel matrix
H1 = Rician(H1_los,FSPL_1,no_mat,K);
% RIS-RX channel matrix
H2_los = exp(-1i*k*d2); % RIS-RX link, LOS matrix exponents
FSPL_2 = 1;
H2 = Rician(H2_los,FSPL_2,no_mat,K);
end
function pos = RISPosition(N1,N2,dist,center) % Determine positions of RIS elements
d1 = (0:N1-1)-(N1-1)/2;
d2 = (0:N2-1)-(N2-1)/2;
pos{1} = center(1)+d1*dist;
pos{2} = center(2)+d2*dist;
end
function Hout = Rician(Hlos,FSPL,no_mat,K) % Create the Rician channel matices
Hlos = repmat(Hlos,no_mat,1);
Hnlos = sqrt(1/2)*(randn(size(Hlos))+1i*randn(size(Hlos)));
Htot = FSPL/sqrt(K+1)*(Hlos*sqrt(K)+Hnlos);
dim = size(Hlos,1)/no_mat;
for ind = 1:no_mat
Hout{ind} = Htot((ind-1)*dim+1:ind*dim,:);
end
end
LoCi
on 6 Jan 2023
I've got this error message
Execution of script varargin as a function is not supported:
C:\Program Files\MATLAB\R2022a\toolbox\matlab\lang\varargin.m
Voss
on 6 Jan 2023
@SAFA AWAD: I copied and pasted your code into an .m file (attached), and it runs without error (until the last line "print('../results/Achievable_Rate', '-dpdf')", which generates an error because I don't have a "../results/" folder).
No error about Unrecognized Pt or execution of varargin is observed.
Here it is running now:
test_script
Error using matlab.graphics.internal.name
Unable to create output file '../results/Achievable_Rate.pdf', No such file or directory.
Unable to create output file '../results/Achievable_Rate.pdf', No such file or directory.
Error in print (line 71)
pj = matlab.graphics.internal.name( pj );
Error in test_script (line 40)
print('../results/Achievable_Rate', '-dpdf')
Voss
on 7 Jan 2023
As I said, the only error I ran into was due to not having a "../results" directory, so the SOLUTION would be, either:
- Create a directory called "results" under the directory one level up from where you are running the code, so that "..\results" exists, or
- Modify the line "print('../results/Achievable_Rate', '-dpdf')" to print to a directory that exists.
Walter Roberson
on 7 Jan 2023
There would have been more to the error message, showing which lines the problem was occurring in. The posted code does not use varargin so the problem must be with something that is being called, but we need the complete error message to figure out what.
LoCi
on 8 Jan 2023
This is exactly the complete error:
Error using matlab.graphics.internal.name
Unable to create output file '../results/Achievable_Rate.pdf', No such file or directory.
Error in print (line 71)
pj = matlab.graphics.internal.name( pj );
Error in test_script (line 40)
print('../results/Achievable_Rate', '-dpdf')
Voss
on 8 Jan 2023
Is there a directory called "results" under the directory one level up from where you are running the code, so that "../results" exists?
Example: if code is running in "/home/projects", print would try to create "/home/results/Achievable_Rate.pdf", so the directory "/home/results" needs to exist beforehand.
Jan
on 8 Jan 2023
@SAFA AWAD: It depends on what "from where I am running the code" means. If this is the current folder:
folder = cd;
If you mean the folder of the M-file:
folder = fileparts(mfilename('fullpath'));
The rest is equal in both cases:
mkdir(fullfile(folder, '..', 'results'))
Voss
on 8 Jan 2023
@SAFA AWAD: Jan is correct; that's how you can do it in MATLAB.
An alternative would be to go into your operating system's File Explorer and create the folder, e.g., on Windows right-click and select 'New Folder' and rename it 'results'.
The point is that the folder has to exist before you try to write a pdf file in it.
Or you can change where the pdf file should go, e.g., to put in the working directory:
print('Achievable_Rate','-dpdf')
Answers (0)
See Also
Categories
Find more on Array Geometries and Analysis 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)