필터 지우기
필터 지우기

different results with fmincon

조회 수: 14 (최근 30일)
guo qing
guo qing 2024년 2월 25일
댓글: guo qing 2024년 2월 26일
I would like to ask about the relationship between the objective function and nonlinear constraint function defined in the fmincon function, as they produce different results. when I write code as follows
x0=[1,1,1,1,1,1];
lb=[0,0,0,0,0,0];
ub=[10,10,10,10,10,10];
B=0.8;
v=30/3.6;
x = fmincon(@(x)fun(x,B,v),x0,[],[],[],[],lb,ub,@(x)nonlcon(x,B,v))
a0 = x(1);
a1 = x(2);
a2 = x(3);
b0 = x(4);
b1 = x(5);
b2 = x(6);
function [f,difference]=fun(x,B,v)% 'difference' is defined
s = tf('s');
w_values = 0:0.5:100;
G = (x(1) + x(2)*s + x(3)*s^2) / (x(4) + x(5)*s + x(6)*s^2);
[mag] = bode(G, w_values);
k_values = zeros(size(w_values));
difference=zeros(size(w_values));% The 'difference' is initialized to array 0
for i = 1:length(w_values)
k = exp(-w_values(i)*B/v);
k_values(i) = k;
end
for i = 1:length(w_values)
difference(i)=abs(mag(i)-k_values(i));% calculate 'difference'
end
f=sum(difference);
end
function [c,ceq] = nonlcon(x,B,v)
[difference]=fun(x,B,v);
%Is the 'difference' here referring to the difference in the function [f,difference]=fun(x,B,v)?"
c=max(difference)-0.001;
ceq=[];
end
results:x =
9.9883 0.0000 0.0021 10.0000 2.6715 0.1221
Directly using "difference" from the objective function as an input parameter for the nonlinear constraint function resulted in different outcomes. Is it reasonable to reference "difference" directly in this way? Do I need to call it like [difference] = fun(x, B, v); as shown in the previous code snippet?
x0=[1,1,1,1,1,1];
lb=[0,0,0,0,0,0];
ub=[10,10,10,10,10,10];
x = fmincon(@fun,x0,[],[],[],[],lb,ub,@nonlcon)%Here it has been altered
a0 = x(1);
a1 = x(2);
a2 = x(3);
b0 = x(4);
b1 = x(5);
b2 = x(6);
function [f]=fun(x)% Here it has been altered
s = tf('s');
B=0.8;
v=30/3.6;
w_values = 0:0.5:100;
G = (x(1) + x(2)*s + x(3)*s^2) / (x(4) + x(5)*s + x(6)*s^2);
[mag] = bode(G, w_values);
k_values = zeros(size(w_values));
difference=zeros(size(w_values));
for i = 1:length(w_values)
k = exp(-w_values(i)*B/v);
k_values(i) = k;
end
for i = 1:length(w_values)
difference(i)=abs(mag(i)-k_values(i));
end
f=sum(difference);
end
function [c,ceq] = nonlcon(difference)%Here it has been altered
c=max(difference)-0.001;
ceq=[];
end
results=
1.0e-03 *
0.7235 0.0006 0.0001 0.8284 0.1887 0.0093

채택된 답변

Matt J
Matt J 2024년 2월 25일
function [c,ceq] = nonlcon(x,B,v)
[~,difference]=fun(x,B,v);
c=max(difference)-0.001;
ceq=[];
end
  댓글 수: 13
Matt J
Matt J 2024년 2월 26일
Do you have any suggestions on how to get better fitting effect
We already know how to get a better fit. Set ub=0.001.
guo qing
guo qing 2024년 2월 26일
Thank you very much. It means that optimizing from the perspective of the total sum of differences is better than setting each individual component of the difference to be less than a certain value. Best wishes.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by