0
votes

I am trying to find out 5 parameters of objective function having 2 equality constraints & 2 inequality constraints with optimization solver "fmincon".

objective function & constraints

I wrote 4 functions to define constraints & 1 function for nonlcon. They are as follows:

Constraint (equation 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

Constraint (equation 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

Constraint (equation 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

Constraint (equation 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

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

Script for optimization:

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))));
x = fmincon(f,[0.5,0.5,0.5,0.5,0.5],[],[],[],[],[],[],[],cons_mo)

After execution, I am getting Invalid indexing assignment error along with other errors. Any help will be much appreciated.

1

1 Answers

0
votes

fmincon is a numerical solver. Your functions are defined in terms of symbolic variables. If you want to solve symbolically, you need to be using solve. However, on my computer, Matlab can't find a symbolic solution to the following:

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

So you need to reformat your functions to take x as a variable:

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