different results with fmincon

13 views (last 30 days)
guo qing
guo qing on 25 Feb 2024
Commented: guo qing on 26 Feb 2024
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

Accepted Answer

Matt J
Matt J on 25 Feb 2024
function [c,ceq] = nonlcon(x,B,v)
[~,difference]=fun(x,B,v);
c=max(difference)-0.001;
ceq=[];
end
  13 Comments
Matt J
Matt J on 26 Feb 2024
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 on 26 Feb 2024
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!