How to solve a minimization problem of a least squares cost function?
9 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Alan Weiss
on 20 Jul 2020
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 Comments
Alan Weiss
on 22 Jul 2020
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!