我试图了解多变量目标优化,所以我需要优化复杂的功能,但是首先,我需要优化以下功能:
function ap_phase = objecfun(tau)
f = 1000; %Frequency
w = 2*pi*f; %Angular Frequency
trans_func = @(taux) (1-1i*w*taux)./(1+1i*w*taux); %Transfer function
trans_zero = trans_func(tau(1)); %Transfer function evaluated with the first variable
trans_quad = trans_func(tau(2)); %Transfer function evaluated with the second variable
ap_phase = rad2deg(phase(trans_zero)-phase(trans_quad)); %Phase difference
end
函数objecfun以一个长度为2的向量作为输入,计算2个传递函数,然后减去传递函数的相位。
我的目标是相位应在90°左右
以下是我用于优化的脚本
tau0 = [2E-5, 1E-3]; %Initial Value for tau(1) and tau(2)
lb = [1E-7, 1E-7]; %Lower bound for tau(1) and tau(2)
ub = [1E-2, 1E-2]; %Upper bound for tau(1) and tau(2)
goal = 90; %Optimization goal
weight = 1; %Weight
[x,fval] = fgoalattain(@objecfun,tau0,goal,weight,[],[],[],[],lb,ub)
优化器收敛,但是我得到了错误的答案,我得到了
x =
0.0100 0.0000
fval =
-178.1044
错了,fval应该在90°附近
我在做什么错了?
答案 0 :(得分:1)
我认为您需要替换您的目标功能和目标值,以使其适合问题的表述。您可以将函数输出与所需角度之差的L2范数用作目标函数,并将目标设置为一定的公差。
我也检查了“ fmincon”:
new_goal = 1e-4;
objectfun = @(x) norm(objecfun(x) - goal);
options = optimoptions('fgoalattain');
options.PlotFcns = 'optimplotfval';
[tau_star,fval] = fgoalattain(objectfun,tau0,new_goal,weight,[],[],[],[],lb,ub,[],options);
options = optimoptions('fmincon');
options.PlotFcns = 'optimplotfval';
[tau_star2,fval,exitflag,output] = fmincon(objectfun,tau0,[],[],[],[],lb,ub,[], options);
fgoalattain_solution_phase_diff = objecfun(tau_star)
fmincon_solution_phase_diff = objecfun(tau_star2)
得到了:
fgoalattain_solution_phase_diff =
90.0000
fmincon_solution_phase_diff =
90.0006
注意:您还可以在函数中省略rad2deg并将其值[rad]用作所需的角度。