# How can I optimize the value of a matrix?

조회 수: 13(최근 30일)
Ahmed elsayed 28 Mar 2020
댓글: Ahmed elsayed 1 Apr 2020
I want to find the optimal B matrix (2*4 matrix) that makes the elements of beta_d (1*4 vector) which is a function of B matrix, equal to the corresponding ones of a "given" beta_u (1*4 vector), i.e. I want to obtain the value of B matrix that makes beta_d(1,1) = beta_u(1,1) && beta_d(1,2) = beta_u(1,2) && beta_d(1,3) = beta_u(1,3) && beta_d(1,4) = beta_u(1,4).
On my attached code, as I am a beginner in MATLAB, I generated random value of B matrix and used 'while loop' to check if my condition is satisfied or not. This way is not efficient and takes very long time to give me a result.
Can anyone help me to optimize the value of B matrix in more efficient and faster way?
% Given H (4*2 matrix)
% Given A (2*2 diagonal matrix)
% Given C (4*4 matrix)
% Given P (4*4 diagonal matrix)
% Given C_l = 4; (constant value)
% Given beta_u (1*4 vector)
% I want to find the optimal B matrix (2*4 matrix) that makes the elements of the 1*4 beta_d vector equal to the corresponding ones of beta_u 1*4 vector
% i.e I want beta_d(1,1) = beta_u(1,1) && beta_d(1,2) = beta_u(1,2) && beta_d(1,3) = beta_u(1,3) && beta_d(1,4) = beta_u(1,4)
%% My non-efficient code:
B = zeros(2,4); % intial value of B 2*4 matrix
beta_d = zeros(1,4); %intial value of beta_d 1*4 vector
% compare each element of beta_d with the corresponding one in beta_u
% optimize the value of B
while(beta_d(1) ~= beta_u(1) || beta_d(2) ~= beta_u(2) || beta_d(3) ~= beta_u(3) || beta_d(4) ~= beta_u(4)) %check the condition
B = randn(2,4); % generate a random 2*4 B matrix
for j=1:1:2 % calculcate the 2*2 diagonal D matrix whose value depends on B
d(j) = (B(j,:)*P*B(j,:)')/((2^(2*C_l))-(norm(A(:,j))^2));
end
D = diag(d);
for i=1:1:4 % calculate beta_d (1*4 vector) whose value depends on B and D
V_d(i)=C(i,:)*P*B'*H(i,:)'*inv(1+H(i,:)*(A'*D*A+B*P*B')*H(i,:)');
sigma_d(i)=norm((V_d(i)*H(i,:)*B-C(i,:))*(P^(1/2)))^2+(V_d(i)^2)*(1+H(i,:)*A'*D*A*H(i,:)');
beta_d(i)=((P(i,i))/sigma_d(:,i));
end
end

#### 댓글 수: 2

you should initialize everything that is inside a for loop which means your variable will initialize for each iteration of the while loop. It also looks like you are checking your guess against a known value of integers. This also takes a long time since you don't give it a tolerance.
Ahmed elsayed 29 Mar 2020
I set a tolerance and it takes a very long time too!
How can I use non-linear optimization to solve my problem?

로그인 to comment.

### 채택된 답변

Rik 29 Mar 2020
편집: Rik 30 Mar 2020
The code below will fit a value for B with the fminsearch function. Note that it is relatively sensitive to a good initial guess. There are still several warnings mlint is giving you, but I will leave those for you to resolve.
edit: added the actual definitions for the input parameters
H = [-0.3208 -0.9784; -1.5994 -1.4689; -1.5197 -0.4568; -0.0993 -0.7667]; % 4*2 matrix
A = [-1 1; 0 1]; % 2*2 matrix
C = [-0.20 0.4 0.6 -0.2; -0.2 0.4 0.6 -0.2; 0.4 0.2 -0.2 0.4; 0.4 0.2 -0.2 0.4]; % 4*4 matrix
P = [250000 0 0 0; 0 250000 0 0; 0 0 250000 0; 0 0 0 250000]; % 4*4 matrix
C_l = 4; % constant value
beta_u = [50.2207 50.2207 20.3433 20.3433]; % 1*4 vector
%store inputs to a struct for shorter syntax
s=struct;[s.H,s.A,s.C,s.P,s.C_l]=deal(H,A,C,P,C_l);
initial_guess=randn(2,4);
OLS=@(b,input_vars)sum((myfun(b,input_vars) - beta_u).^2);%ordinary least squares cost function
opts = optimset('MaxFunEvals',50000, 'MaxIter',10000);
fit_val = fminsearch(OLS, initial_guess, opts,s);
function beta_d=myfun(B,input_vars)
%calculate beta_d from B and the other inputs
s=input_vars;[H,A,C,P,C_l]=deal(s.H,s.A,s.C,s.P,s.C_l);
beta_d = zeros(1,4); %intial value of beta_d 1*4 vector
for j=1:1:2 % calculcate the 2*2 diagonal D matrix whose value depends on B
d(j) = (B(j,:)*P*B(j,:)')/((2^(2*C_l))-(norm(A(:,j))^2));
end
D = diag(d);
for i=1:1:4 % calculate beta_d (1*4 vector) whose value depends on B and D
V_d(i)=C(i,:)*P*B'*H(i,:)'*inv(1+H(i,:)*(A'*D*A+B*P*B')*H(i,:)');
sigma_d(i)=norm((V_d(i)*H(i,:)*B-C(i,:))*(P^(1/2)))^2+(V_d(i)^2)*(1+H(i,:)*A'*D*A*H(i,:)');
beta_d(i)=((P(i,i))/sigma_d(:,i));
end
end

#### 댓글 수: 15

표시 이전 댓글 수: 12
Rik 30 Mar 2020
You know the values of your variables. You can save your variables as normal.
So if I understand you correctly, my answer solved your question now?
Ahmed elsayed 30 Mar 2020
Yes, thank you for your efforts. I really apperiate it.
Ahmed elsayed 1 Apr 2020
If I can ask you another question:
I have 4 users, and I want to simulate the outage probability which is the probability of the user data rate is less than the predefined threshold value i.e. if the threshold rate per user is = 0.512.
After changing the signal to noise ratio SNR from -20 dB to 30 dB, I obtain the rate matrix with dimensions (number of users × the length of SNR vector), where each element in this matrix represents the rate of certain user at specific SNR value as follows:
SNR_vector_dB=-20:10:30; %the SNR vector
R_th = 0.512; % the user data rate threshold value
% Rate matrix with dimensions (number of users =4 * the length of SNR vector=6)
R = [0.0021 0.0191 0.1466 0.7368 1.5617 1.9838; ...
0.0021 0.0191 0.1466 0.7368 1.5617 1.9838; ...
0.0041 0.0370 0.3190 0.9267 1.6591 2.1856; ...
0.0041 0.0370 0.3190 0.9267 1.6591 2.1856];
Can you help me in simulating the outage probability versus SNR?

로그인 to comment.

### 추가 답변(0개)

이 질문에 답변하려면 로그인을(를) 수행하십시오.

Translated by