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
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
Sara MS
2020년 7월 22일
Sara MS
2020년 7월 22일
Alan Weiss
2020년 7월 22일
Alan Weiss
MATLAB mathematical toolbox documentation
Sara MS
2020년 7월 22일
Alan Weiss
2020년 7월 22일
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?
Alan Weiss
MATLAB mathematical toolbox documentation
카테고리
도움말 센터 및 File Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!