why do I always receive the error: Unrecognized function or variable 'Pt'.
2 次查看(过去 30 天)
显示 更早的评论
LoCi
2023-1-5
function [Cout,iter_time] = PGM_opt(Pt,Hdir,H1,H2,maxIter,Qinit,myomegaunitball,c)
17 个评论
Dyuman Joshi
2023-1-5
编辑:Dyuman Joshi
2023-1-5
Please show your full code, how you call the function and the full error.
LoCi
2023-1-5
编辑:Voss
2023-1-6
% 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
2023-1-5
You still haven't attached how you call the function and what the full error is.
LoCi
2023-1-6
编辑:Voss
2023-1-6
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
2023-1-6
编辑:Voss
2023-1-6
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
2023-1-6
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
2023-1-6
@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
2023-1-7
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
2023-1-7
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
2023-1-8
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
2023-1-8
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
2023-1-8
@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
2023-1-8
@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')
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Array Geometries and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)