different results with fmincon
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Octave에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!