2017-02-16 34 views
0

我想找出具有2個等式約束的目標函數的5個參數& 2使用優化求解器「fmincon」的不等式約束。在Matlab中使用fmincon進行非線性優化時,無效的索引分配錯誤

objective function & constraints

我寫4個函數來定義約束nonlcon & 1功能。它們如下:

約束(公式3):

function [C1] = s_1(w) 
x = sym('x', [1 5]); 
GC = (x(1)+x(2)/(j*w)^x(3)+x(4)*(j*w)^x(5))*(0.55/(62*j*w+1)); 
C1 = rad2deg(angle(GC))+100; 
end 

約束(公式4):

function [C2] = s_2(w) 
x = sym('x', [1 5]); 
GC = (x(1)+x(2)/(j*w)^x(3)+x(4)*(j*w)^x(5))*(0.55/(62*j*w+1)); 
GC1 = angle(GC); 
C2 = diff(GC1,w); 
end 

約束(公式5):

function [T1] = s_4(w) 
x = sym('x', [1 5]); 
GC = (x(1)+x(2)/(j*w)^x(3)+x(4)*(j*w)^x(5))*(0.55/(62*j*w+1)); 
T = GC/(1+GC); 
T1 = 20*log10(abs(T)) + 20; 
end 

約束(等式6)

function [S1] = s_5(w) 
x = sym('x', [1 5]); 
GC = (x(1)+x(2)/(j*w)^x(3)+x(4)*(j*w)^x(5))*(0.55/(62*j*w+1)); 
S = 1/(1+GC); 
S1 = 20*log10(abs(S)) + 20; 
end 

'nonlcon' 功能

function [ c,ceq ] = cons_mo(x) 
ceq(1) =(s_1(1)); 
ceq(2) =(s_2(1)); 
c(1) = (s_4(10)); 
c(2) = (s_5(0.01)) ; 
end 

腳本優化:

[email protected](x)20*log10(abs((x(1)+x(2)/(j*1)^x(3)+x(4)*(j*1)^x(5))*(0.55/(62*j*1+1)))); 
x = fmincon(f,[0.5,0.5,0.5,0.5,0.5],[],[],[],[],[],[],[],cons_mo) 

執行後,我與其他錯誤相處無效索引分配錯誤。任何幫助都感激不盡。

回答

0

fmincon是一個數值求解器。你的函數是用符號變量來定義的。如果你想象徵性地解決,你需要使用solve。然而,在我的電腦上,MATLAB不能找到一個象徵性的解決方案如下:

function x = solveme() 
x = sym('x', [1 5]); 
[c,ceq] = cons_mo(x); 
f = 20*log10(abs((x(1)+x(2)/(j*1)^x(3)+x(4)*(j*1)^x(5))*(0.55/(62*j*1+1)))); 
% x = fmincon(f,[0.5,0.5,0.5,0.5,0.5],[],[],[],[],[],[],[],cons_mo) 
x = solve([f,c,ceq],x); 
end 

function [C1] = s_1(w) 
x = sym('x', [1 5]); 
GC = (x(1)+x(2)/(j*w)^x(3)+x(4)*(j*w)^x(5))*(0.55/(62*j*w+1)); 
C1 = 180/pi*(angle(GC))+100; 
end 

function [C2] = s_2(w) 
x = sym('x', [1 5]); 
GC = (x(1)+x(2)/(j*w)^x(3)+x(4)*(j*w)^x(5))*(0.55/(62*j*w+1)); 
GC1 = angle(GC); 
C2 = diff(GC1,w); 
end 


function [T1] = s_4(w) 
x = sym('x', [1 5]); 
GC = (x(1)+x(2)/(j*w)^x(3)+x(4)*(j*w)^x(5))*(0.55/(62*j*w+1)); 
T = GC/(1+GC); 
T1 = 20*log10(abs(T)) + 20; 
end 


function [S1] = s_5(w) 
x = sym('x', [1 5]); 
GC = (x(1)+x(2)/(j*w)^x(3)+x(4)*(j*w)^x(5))*(0.55/(62*j*w+1)); 
S = 1/(1+GC); 
S1 = 20*log10(abs(S)) + 20; 
end 


function [ c,ceq ] = cons_mo(x) 
ceq(1) =(s_1(1)); 
ceq(2) =(s_2(1)); 
c(1) = (s_4(10)); 
c(2) = (s_5(0.01)) ; 
end 

所以,你需要重新格式化功能,從而利用x作爲變量:

function x = solveme() 
f = @(x)20*log10(abs((x(1)+x(2)/(j*1)^x(3)+x(4)*(j*1)^x(5))*(0.55/(62*j*1+1)))); 
aa = 1; 
bb = 1; 
cc = 10; 
dd = 0.01; 
x = fmincon(f,[0.5,0.5,0.5,0.5,0.5],[],[],[],[],[],[],@cons_mo) 

    function [ c,ceq ] = cons_mo(x) 
     ceq(1) =(s_1(x,aa)); 
     ceq(2) =(s_2(x,bb)); 
     c(1) = (s_4(x,cc)); 
     c(2) = (s_5(x,dd)) ; 
    end 

end 

function [C1] = s_1(x,w) 
GC = (x(1)+x(2)/(j*w)^x(3)+x(4)*(j*w)^x(5))*(0.55/(62*j*w+1)); 
C1 = 180/pi*(angle(GC))+100; 
end 

function [C2] = s_2(x,w) 
% GC = (x(1)+x(2)/(j*w)^x(3)+x(4)*(j*w)^x(5))*(0.55/(62*j*w+1)); 
% GC1 = angle(GC); 
% C2 = diff(GC1,w); 
C2 = (real((x(1) + x(4)*(w*1i)^x(5) + x(2)/(w*1i)^x(3))/(1 + w*62i))^2*((imag(-((x(1) + x(4)*(w*1i)^x(5) + x(2)/(w*1i)^x(3))*62i)/(1 + w*62i)^2) - imag((x(2)*x(3) - x(4)*x(5)*(w*1i)^(x(3) + x(5)))/(w*(1 + w*62i)*(w*1i)^x(3))))/real((x(1) + x(4)*(w*1i)^x(5) + x(2)/(w*1i)^x(3))/(1 + w*62i)) - (imag((x(1) + x(4)*(w*1i)^x(5) + x(2)/(w*1i)^x(3))/(1 + w*62i))*(real(-((x(1) + x(4)*(w*1i)^x(5) + x(2)/(w*1i)^x(3))*62i)/(1 + w*62i)^2) - real((x(2)*x(3) - x(4)*x(5)*(w*1i)^(x(3) + x(5)))/(w*(1 + w*62i)*(w*1i)^x(3)))))/real((x(1) + x(4)*(w*1i)^x(5) + x(2)/(w*1i)^x(3))/(1 + w*62i))^2))/(imag((x(1) + x(4)*(w*1i)^x(5) + x(2)/(w*1i)^x(3))/(1 + w*62i))^2 + real((x(1) + x(4)*(w*1i)^x(5) + x(2)/(w*1i)^x(3))/(1 + w*62i))^2); 
end 


function [T1] = s_4(x,w) 
GC = (x(1)+x(2)/(j*w)^x(3)+x(4)*(j*w)^x(5))*(0.55/(62*j*w+1)); 
T = GC/(1+GC); 
T1 = 20*log10(abs(T)) + 20; 
end 


function [S1] = s_5(x,w) 
GC = (x(1)+x(2)/(j*w)^x(3)+x(4)*(j*w)^x(5))*(0.55/(62*j*w+1)); 
S = 1/(1+GC); 
S1 = 20*log10(abs(S)) + 20; 
end 
+0

非常感謝。你能告訴我,你是怎麼計算C2的? – basic123

+0

你已經做了符號區分的原始代碼。我只複製了輸出,並在字符串中進行了一些替換,例如x(1)而不是x1。 –