Why this code the given error?

close all; clear all; clc;
vec = @(MAT) MAT(:);
vecH = @(MAT) MAT(:).';
% M = 5;
M = 10;
N = 4;
% N = 10;
d = 0.5;
K = 3;
SNR_dB = 20;
steerVecT = @(ang) exp(1j*2*pi*d*[0:M-1].'*sin(vecH(ang)));
steerVecR = @(ang) exp(1j*2*pi*d*[0:N-1].'*sin(vecH(ang)));
angleGrid = [-80:1e-2:80].';
steerGrid = steerMatTR(deg2rad(angleGrid), steerVecT, steerVecR);
rng(222);
targetAngle = [-30, 0, 40].'+rand(K, 1);
A = ones(K, 1);
steerM = steerMatTR(deg2rad(targetAngle), steerVecT, steerVecR);
r = steerM*A;
noiseVar = r' * r / length(r) / db2pow(SNR_dB);
w = sqrt(noiseVar / 2) * (randn(size(r)) + 1j * randn(size(r)));
y = r+w;
lambda = 2*sqrt(M*N*log(M*N))*sqrt(noiseVar);
cvx_begin sdp quiet
variable x(N*M,1) complex
variable F(M*N,M*N) hermitian
minimize(norm(y-x))
subject to
[F, x; x', lambda^2]>=0
for idxN1 = 0 : 1 : N-1
for idxN2 = 0 : 1 : N-1
Ftmp = F(idxN1*M+1:idxN1*M+M, idxN2*M+1:idxN2*M+M);
for delta = -(M-1) : 1 : M-1
if ((idxN1==idxN2) && (delta==0))
sum(diag(Ftmp, delta)) <= 1/N;
else
sum(diag(Ftmp, delta)) == 0;
end
end
end
end
cvx_end
spectrumANM = abs(x'*steerGrid).^2;
spectrumANM = spectrumANM/max(spectrumANM);
figure; plot(angleGrid, spectrumANM);
% User defined function
function steerM = steerMatTR(targetAngle, steerVecT, steerVecR)
steerA = steerVecT(targetAngle);
steerB = steerVecR(targetAngle);
steerM = zeros(size(steerA, 1)*size(steerB, 1), length(targetAngle));
for idxK = 1 : 1 : length(targetAngle)
steerM(:, idxK) = kron(steerB(:, idxK), steerA(:, idxK));
end
After running the above code, it gives the following error:
Undefined function 'cvx_begin' for input arguments of type 'char'.
Error in main (line 32)
cvx_begin sdp quiet
Can anyone help me in this regard?

3 Comments

Did you install CVX? Is it correctly added to the path?
Thanks for the responses of both of you dear DGM and Adam Danz. No, I don't know anything about CVX? what is that and how to install that?

Sign in to comment.

 Accepted Answer

the cyclist
the cyclist on 8 Sep 2021
CVX is a third-party MATLAB toolbox (i.e. not from MathWorks), that you need to install according to their instructions. You need to go to their website to do that.

11 Comments

Thanks a lot. I visited the site and downloaded the "cvx-w64.zip" for windows, then extracted it and then kept my above two files also in this folder. Then I ran again the above program, but again it gave the same error as above.
There are step-by-step installation instructions in the documentation of CVX. Did you follow all of those instructions?
Thank you very much the cyclist. Yes, now I did it according to the instructions and it worked. Thanks to all of you for helping me out.
Another question is why the instruction rng(222) has been used here in the code. rng is a random number generator but why 222 and whats the meaning of this 222 here?
There is unlikely to be any particular meaning to the 222. It was probably an arbitrary choice, just like chosing 1234 or 12345 would have been. The author needed to choose some number in order to have the code always produce the same output for demonstration purposes.
Thanks a lot dear Walter Roberson! Actually this is not my code.I understand some basic commands but some are very strange for me. For example I don't know what are the following commands doing?
vec = @(MAT) MAT(:);
steerVecT = @(ang) exp(1j*2*pi*d*[0:M-1].'*sin(vecH(ang)));
cvx_begin sdp quiet
variable x(N*M,1) complex
variable F(M*N,M*N) hermitian
minimize(norm(y-x))
subject to
[F, x; x', lambda^2]>=0
for idxN1 = 0 : 1 : N-1
for idxN2 = 0 : 1 : N-1
Ftmp = F(idxN1*M+1:idxN1*M+M, idxN2*M+1:idxN2*M+M);
for delta = -(M-1) : 1 : M-1
if ((idxN1==idxN2) && (delta==0))
sum(diag(Ftmp, delta)) <= 1/N;
else
sum(diag(Ftmp, delta)) == 0;
end
end
end
end
cvx_end
Can you help me in understanding these commands?
vec = @(MAT) MAT(:);
That creates an anonymous function that accepts a single input parameter, and reshapes the input parameter to be a column vector. For example
A = [1 2; 3 4]
A = 2×2
1 2 3 4
vec(A)
ans = 4×1
1 3 2 4
The vecH anonymous function does something very similar, but reshapes to a row vector instead.
steerVecT = @(ang) exp(1j*2*pi*d*[0:M-1].'*sin(vecH(ang)));
This is an anonmous function that accepts a single parameter.
1j is the imaginary constant, 1*sqrt(-1). 2*pi are what they look like. d is a scalar constant.
[0:M-1] creates the row vector containing 0, 1, 2, 3, 4, up to M-1 -- up to 9 in this case. Then the [] operator around it causes it to be concatenated with the additional input values, but there are no additional input values, so the [] operator is a waste of time here. The [0:M-1] would have been better written as (0:M-1) .
Notice that [] is an active operation in MATLAB, having to do with building vectors and matrices. It is not used to establish priorities for evaluation (though it does have that side-effect)
The .' after the [0:M-1] takes the row vector and converts it into a column vector.
At this point in the expression, the 1j*2*pi*d has been calculated into a single (complex) scalar value. The [0:M-1].' on the right hand side of the * operation has priority over the * operation so the [0:M-1].' is evaluated before the multiplication is.
Now at this point you have scalar * column vector of integers, 0 to M. The first row of output would be 0*constant, the second row would be 1*constant, and so on up to (M-1)*constant, all as a column vector.
We have now reached the * between the .' and the sin() and have evaluated the left side of that to a column vector. The right side now needs to be evaluated.
The vecH(ang) reshapes the function input into a row vector. sin() of the row vector is calculated.
Now we have column vector * row vector. The * operator in MATLAB is algebraic matrix multiplication, also called inner product. In this particular arrangement of column vector * row vector, the mathematical effect is to take the dot product of the column vector and the row vector . Which is to say, to take the sum of multiplying the corresponding elements: C(1).*R(1) + C(2).*R(2) + C(3).*R(3) ... The result of this is going to be a scalar.
And now we calculate exp() of that scalar.
... but why you calculate that is a different question.
cvx_begin sdp quiet
variable x(N*M,1) complex
variable F(M*N,M*N) hermitian
minimize(norm(y-x))
subject to
[F, x; x', lambda^2]>=0
those lines all have to do with the third-party program cvx which is a convex optimization program.
If I read correctly, the lines after that until the cvx_end are there to establish constraints for the optimization that is cvx is being asked to do.
Thanks a lot dear Walter Roberson for your so detailed help. Now I can understand it very well. Indeed, we, the weak people need the guiders like you. Thank you very much once again dear Walter Roberson.
Dear Walter Roberson how can I accept your answer as there is no option in front of your name?
@Sadiq Akbar, you won't be able to officially accept @Walter Roberson's. The reason is that I answered your original question (so you could accept that), but then you asked additional questions in the ensuing comments. Walter was kind enough to answer those additional questions in his comments, but unfortunately comments cannot be accepted or upvoted.
Thank you very much dear the cyclist for your guidance. Ok I am going to accept that. But actually I am thankful to all of you as I have mentioned earlier.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!