Optimize Function with two sums using fmincon
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hey everybody,
i have to calculate the a(i) to optimize the following function
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/614630/image.png)
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
0 commentaires
Réponse acceptée
Matt J
le 11 Mai 2021
Modifié(e) : Matt J
le 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)
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!