How to solve a minimization problem of a least squares cost function?

I want to find B (2*2 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), for example: 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).
So, I started using 'fminunc' to find the value of B (2*2 matrix) that minimzes the difference between the corresponding elements in the two vectors. However, it doesn't give me the desired result.
I want to know what is wrong on my code?
clc;
clear;
% System paramters:
c_l = 4;
H = [0.7182 -1.9924; 0.8306 0.0195; -0.6868 -0.7119; -0.1692 0.1084]; % 4*2 matrix
A = [-3 1; 1 0]; % 2*2 matrix
C = [1 -2; 0 1; 1 -2; 0 1]; % 4*2 matrix
P_u = 25*eye(4); % 4*4 diagonal matrix
P_d = 25*eye(2); % 2*2 diagonal matrix
beta_u = [50.4551 59.1605 50.4551 59.1605]; % 1*4 vector
beta_d = zeros(1,4); % intial 1*4 vector
%store inputs to a struct for shorter syntax
s=struct;
[s.H,s.A,s.C,s.P_u,s.P_d,s.C_l]=deal(H,A,C,P_u,P_d,c_l);
% Minmization optimization
while (sum(abs(beta_d-beta_u))>0.1)
initial_guess = randn(2,2);
OLS = @(B,input_vars)sum(abs(myfun(B,input_vars)-beta_u).^2); % ordinary least squares cost function
opts = optimoptions(@fminunc, 'MaxIterations', 10000, 'MaxFunctionEvaluations', 50000, 'Display', 'Iter', 'FiniteDifferenceStepSize', 1e-3);
[B,FVAL] = fminunc(OLS, initial_guess, opts,s);
input_vars = s;
[beta_d,D_d]= myfun(B,input_vars);
end
%calculate beta_d from B and the other inputs
function [beta_d,D_d]=myfun(B,input_vars)
%load parameters
s=input_vars;[H,A,C,P_u,P_d,C_l]=deal(s.H,s.A,s.C,s.P_u,s.P_d,s.C_l);
for j=1:1:2
d(j) = (B(j,:)*P_d*B(j,:)')/((2^(2*C_l))-(norm(A(:,j))^2));
end
D_d = diag(d);
for i=1:1:4
V_d(i)=C(i,:)*P_d*B'*H(i,:)'*inv(1+H(i,:)*(A'*D_d*A+B*P_d*B')*H(i,:)');
sigma_d(i)=norm((V_d(i)*H(i,:)*B-C(i,:))*(P_d^(1/2)))^2+(V_d(i)^2)*(1+H(i,:)*A'*D_d*A*H(i,:)');
beta_d(i)=((P_u(i,i))/sigma_d(:,i));
end
end

답변 (1개)

Alan Weiss
Alan Weiss 2020년 7월 20일

1 개 추천

To solve a set of nonlinear equations, I suggest that you use fsolve instead of fminunc. For fsolve your objective function should not compute the sum of squares, but should return the vector of function values minus the vector that you are trying to equal. In other words, fsolve sovles , where G is a vector-valued function. To solve , instead solver , where .
Alan Weiss
MATLAB mathematical toolbox documentation

댓글 수: 5

Thanks for your answer. Sorry, I can't understand your answer well. Can you explain it by making the required edits on my attached code?
If I understand well:
This is my code after modification:
The code doesn't give me output and it seems that I am in an infintie loop!!
Can you help me to find what's the problem?
clc;
clear;
% System paramters:
c_l = 4;
H = [0.7182 -1.9924; 0.8306 0.0195; -0.6868 -0.7119; -0.1692 0.1084]; % 4*2 matrix
A = [-3 1; 1 0]; % 2*2 matrix
C = [1 -2; 0 1; 1 -2; 0 1]; % 4*2 matrix
P_u = 25*eye(4); % 4*4 diagonal matrix
P_d = 25*eye(2); % 2*2 diagonal matrix
beta_u = [50.4551 59.1605 50.4551 59.1605]; % 1*4 vector
beta_d = zeros(1,4); % intial 1*4 vector
%store inputs to a struct for shorter syntax
s=struct;
[s.H,s.A,s.C,s.P_u,s.P_d,s.C_l]=deal(H,A,C,P_u,P_d,c_l);
% Minmization optimization
iter = 0;
while (sum(abs(beta_d-beta_u))>0.1)
iter= iter+1
initial_guess = randn(2,2);
OLS = @(B,input_vars)((myfun(B,input_vars)-beta_u)); % ordinary least squares cost function
opts = optimoptions('fsolve', 'MaxIterations', 10000, 'MaxFunctionEvaluations', 50000, 'FiniteDifferenceStepSize', 1e-3);
[B,FVAL] = fsolve(OLS, initial_guess, opts,s);
input_vars = s;
[beta_d,D_d]= myfun(B,input_vars);
end
%calculate beta_d from B and the other inputs
function [beta_d,D_d]=myfun(B,input_vars)
%load parameters
s=input_vars;[H,A,C,P_u,P_d,C_l]=deal(s.H,s.A,s.C,s.P_u,s.P_d,s.C_l);
for j=1:1:2
d(j) = (B(j,:)*P_d*B(j,:)')/((2^(2*C_l))-(norm(A(:,j))^2));
end
D_d = diag(d);
for i=1:1:4
V_d(i)=C(i,:)*P_d*B'*H(i,:)'*inv(1+H(i,:)*(A'*D_d*A+B*P_d*B')*H(i,:)');
sigma_d(i)=norm((V_d(i)*H(i,:)*B-C(i,:))*(P_d^(1/2)))^2+(V_d(i)^2)*(1+H(i,:)*A'*D_d*A*H(i,:)');
beta_d(i)=((P_u(i,i))/sigma_d(:,i));
end
end
I suggest that you use the debugger to examine what is going on in your program.
Alan Weiss
MATLAB mathematical toolbox documentation
I used the debugger and I found that the solver can't find a solution. Is there any way that can help me solving the problem? Please, help me.
Not every nonlinear equation has a real solution. For example, has no real solution. Are you sure that your equation, as written, has a solution?
For suggestions about looking for a solution, see fsolve Could Not Solve Equation.
Alan Weiss
MATLAB mathematical toolbox documentation

댓글을 달려면 로그인하십시오.

카테고리

질문:

2020년 7월 17일

댓글:

2020년 7월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by