我想找到B(22矩阵(,它使作为B矩阵函数的beta_ d(14向量(的元素等于a";给定";beta_(14向量(,即beta_&beta_(1,2(=beta_&beta_(1,3(=beta_&β_d(1.4(=β_u(1,4(。因此,我开始使用"fminunc"来找到B(22矩阵(的值,该值最小化了两个向量中相应元素之间的差异。然而,它通常会给我以下错误:
Error using lineSearch
Search direction is not a descent direction; roundoff errors may be affecting convergence.
Error in fminusub (line 186)
lineSearch(funfcn,x,dir,f,dirDerivative, ...
Error in fminunc (line 472)
[x,FVAL,GRAD,HESSIAN,EXITFLAG,OUTPUT] = fminusub(funfcn,x, ...
有人能帮我解决这个"fminuc"问题吗?或者建议另一种方法来找到B(2*2矩阵(的值,该值最小化两个向量beta_和beta_?我的代码是:
clc;
clear;
% System paramters:
c_l = 4;
counter = 0;
H = [0.7182 -1.9924; 0.8306 0.0195; -0.6868 -0.7119; -0.1692 0.1084];
A = [-3 1; 1 0];
C = [1 -2; 0 1; 1 -2; 0 1];
P_u = 25*eye(4);
P_d = 25*eye(2);
beta_u = [50.4551 59.1605 50.4551 59.1605];
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_u,s.P_d,s.C_l]=deal(H,A,C,P_u,P_d,c_l);
while (sum(abs(beta_u-beta_d))>0.1 && counter< 500)
initial_guess = randn(2,2);
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_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
将'Display','Iter'
添加到optimoptions
调用中,这样您就可以查看优化器正在执行的操作的更多详细信息。优化器可能进入了条件较差的区域,或者接近局部最小值或鞍点,导致成本函数梯度的有限差分近似表现不佳。
一个建议是尝试在optimoptions
调用中将FiniteDifferenceStepSize
设置为某个小值。
这里有一个例子:
optimoptions(@fminunc, 'MaxIterations', 10000, 'MaxFunctionEvaluations', 50000, 'Display', 'Iter', 'FiniteDifferenceStepSize', 1e-3);