MATLAB: How to optimize the value of a matrix

matrixoptimizationvectorwhile loop

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

Best Answer

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
%load parameters
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