Optimize Function with two sums using fmincon

Hey everybody,
i have to calculate the a(i) to optimize the following function
k = [5,7,11,13]
p = [1:6]
the a(i) have to be between [0,pi/4]. I want to use the fmincon function, but i dont know how to programm the function d. My first try was the following:
k = [5,7,11,13];
p = [1:4];
a0 = [pi/10, pi/8, pi/7, pi/5];
f1 = sum(1./k.^4)*(1.+2.*sum((-1).^p.*cos(k.*a(p)))).^2;
f2 = sum(1./k.^4);
obj = sqrt(f1./f2);
A = [];
B = [];
Aeq = [];
Beq = [];
lb = [0,0,0,0];
ub = [pi/4,pi/4,pi/4,pi/4];
x = fmincon(Sum, a0, A, B, Aeq, Beq, lb, ub);
But this is not working at all. "Error using optimfcnchk (line 117) FUN must be a function, a valid character vector expression, or an inline function object."
Furthermore the code does not provide an overall combination of the parameters, so i tried it this way
Sum = 0;
a = [pi/8, pi/7, pi/6, pi/5];
for k = [5,7,11]
for p = [1:4]
Sum = Sum + sqrt(((1/k.^4) * (1+2*(-1).^p .*cos(k*a(p)).^2) / (1/k^4)));
end
end
A = [];
B = [];
Aeq = [];
Beq = [];
lb = [0,0,0,0];
ub = [pi/4,pi/4,pi/4,pi/4];
x = fmincon(Sum, a, A, B, Aeq, Beq, lb, ub);
But now the function cant be used in fmincon.
Can somebody help me how i can implement the desired funnktion to be optimized with fmincon?
Regards

 Akzeptierte Antwort

Matt J
Matt J am 11 Mai 2021
Bearbeitet: Matt J am 12 Mai 2021
k = [5,7,11,13];
p = [1:4];
a0 = [pi/10, pi/8, pi/7, pi/5];
weights=(1./k.^4);
weights=weights/sum(weights);
fun = @(a) sum( weights(:).*( 1 + 2*sum( (-1).^p.*cos(k(:).*a) ,2) ).^2 );
A = [];
B = [];
Aeq = [];
Beq = [];
lb = [0,0,0,0];
ub = [pi/4,pi/4,pi/4,pi/4];
x = fmincon(fun, a0, A, B, Aeq, Beq, lb, ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 1×4
0.1841 0.2809 0.5394 0.5736

4 Kommentare

Meikel Vollmers
Meikel Vollmers am 12 Mai 2021
Bearbeitet: Meikel Vollmers am 12 Mai 2021
Thanks for the answer, but i think theres still a little mistake. Just for understanding: d is the distortion factor of a sinusoidal wave. k is the harmonic order.
p is the amount of commutation angles over one period in my Inverter, and i want to know for which angles d is minimized.
When i reduce the number of harmonics to be eliminated, i.e. i want to minimize the THD of the 5,7 and 11th harmonic order (so k= [5,7,11]), and i want to to this with at least 6 commutation angles (p=6), this isnt possible because of the matrix dimensions. So in my understanding the code works wrong because he reduces the function when k = 5 only for p=1, when k=7 only for p=2 and so on, requested is a reduction for k = 5 with p=1:6 and for k=7 with p=1:6 also.
I was searching for the right word. What i need is that the code respects the principle of nested sums.
Matt J
Matt J am 12 Mai 2021
Bearbeitet: Matt J am 12 Mai 2021
Small fix:
fun = @(a) sum( weights(:).*( 1 + 2*sum( (-1).^p.*cos(k(:).*a) ,2) ).^2 );
Hey Matt, the fix seems to be working. Now i want to implement a constraint into my function.
and m will be chosen to i.e. 0.7.
I tried the nonlinear constraint this way:
function [c,ceq] = modindex(a,p)
c = [];
ceq = 4/pi.*( 1 + 2*sum( (-1).^p.*cos(a(p)) ,2 ))-0.7;
end
nlcon = @modindex;
x = fmincon(fun, a0, A, B, Aeq, Beq, lb, ub, nlcon)
But the error is "Not enough input arguments." What am i doing wrong?
Matt J
Matt J am 17 Mai 2021
Bearbeitet: Matt J am 17 Mai 2021
It needs to be,
nlcon = @(a) modindex(a,p);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by