Using 'fminunc', I receive Optimization completed because the size of the gradient is less than the value of the optimality tolerance.

33 次查看(过去 30 天)
I use 'fminunc' to find the value of B (2*4 matrix) that minimzes the difference between the corresponding elements in two vectors as indicated in the attached code. In other words, I want to find the B (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. 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).
However, I usually receive the following message without getting any result and the program seems to go on an infinite loop!
Local minimum found.
Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.
<stopping criteria details>
the code:
clc;
clear;
% System paramters:
N = 2;
K = 4;
C_l = 4;
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
beta_u = [50.2207 50.2207 20.3433 20.3433]; % 1*4 vector
beta_d = zeros(1,4); % intial value
B = zeros(2,4); %intial value
%store inputs to a struct for shorter syntax
s=struct;[s.H,s.A,s.C,s.P,s.C_l,s.N,s.K]=deal(H,A,C,P,C_l,N,K);
%fminunc optimization
while (sum(abs(beta_u-beta_d))>=0.1)
initial_guess = randn(2,4);
OLS = @(B_d,input_vars)sum((beta_u-myfun(B_d,input_vars)).^2);%ordinary least squares cost function
opts = optimoptions(@fminunc,'MaxIterations',10000,'MaxFunctionEvaluations',50000,'CheckGradients',true);
B = 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,C_l,N,K]=deal(s.H,s.A,s.C,s.P,s.C_l,s.N,s.K);
for j=1:1:N
d(j) = (B(j,:)*P*B(j,:)')/((2^(2*C_l))-(norm(A(:,j))^2));
end
D_d = diag(d);
for i=1:1:K
V_d(i)=C(i,:)*P*B'*H(i,:)'*inv(1+H(i,:)*(A'*D_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_d*A*H(i,:)');
beta_d(i)=((P(i,i))/sigma_d(:,i));
end
end

采纳的回答

Matt J
Matt J 2020-5-21
编辑:Matt J 2020-5-21
If the problem doesn't have a solution satisfying the tolerance condition in your while loop
(sum(abs(beta_u-beta_d))<0.1)
then the loop has no way of stopping.
  10 个评论
Ahmed Nasr
Ahmed Nasr 2020-5-22
This code sometimes gives me a result, and sometimes gives me the previous mentioned error!
clc;
clear;
% System paramters:
N = 2;
K = 4;
C_l = 4;
counter = 0;
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
beta_u = [50.2207 50.2207 20.3433 20.3433]; % 1*4 vector
beta_d = zeros(1,4); % intial value
%store inputs to a struct for shorter syntax
s=struct;[s.H,s.A,s.C,s.P,s.C_l,s.N,s.K]=deal(H,A,C,P,C_l,N,K);
while (sum(abs(beta_u-beta_d))>0.1 && counter< 500)
initial_guess = randn(2,4);
OLS = @(B_d,input_vars)sum(abs(beta_u-myfun(B_d,input_vars)).^2);%ordinary least squares cost function
opts = optimoptions(@fminunc,'MaxIterations',10000,'MaxFunctionEvaluations',50000);
[B,FVAL,grad] = fminunc(OLS, initial_guess, opts,s);
input_vars = s;
[beta_d, D_d]=myfun(B,input_vars);
counter = counter+1;
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,C_l,N,K]=deal(s.H,s.A,s.C,s.P,s.C_l,s.N,s.K);
for j=1:1:N
d(j) = (B(j,:)*P*B(j,:)')/((2^(2*C_l))-(norm(A(:,j))^2));
end
D_d = diag(d);
for i=1:1:K
V_d(i)=C(i,:)*P*B'*H(i,:)'*inv(1+H(i,:)*(A'*D_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_d*A*H(i,:)');
beta_d(i)=((P(i,i))/sigma_d(:,i));
end
end
Matt J
Matt J 2020-5-24
编辑:Matt J 2020-5-24
fminunc is not succeeding in approximating the gradient of your objective function accurately using its default finite difference method. You could try supplying your own gradient calculation using the SpecifyObjectiveGradient option,

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile 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!

Translated by