Function optimization meeting some conditions

5 vues (au cours des 30 derniers jours)
Jon Bilbao
Jon Bilbao le 8 Juin 2023
Modifié(e) : Jon le 8 Juin 2023
I have a array ht(i,j) and i want to calculate de values that minimize de sumatory of sum((hti,j)-h(j).^2), meeting the conditions: h(j)<120, h(j+1)>h(j) and h(j+1)-h(j)<1.25. I am trying doing it with the optimizetool but i don´t know how, and using fmincon i have done this:
function [h] = hp3(ht)
[n,m]=size(ht);
Ins = @(h) sum((ht - h).^2);
h0 = zeros(size(m));
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
h = fmincon(Ins, h0, A, b, Aeq, beq, lb, ub, @constraints);
end
function [c, ceq] = constraints(h)
c = h(2:end) - h(1:end-1);
ceq = [];
end
  1 commentaire
Jon
Jon le 8 Juin 2023
Also your line of code:
h0 = zeros(size(m));
Doesn't look correct, m is a scalar so size(m) will by 1,1
I think you want
h0 = zeros(m,1);

Connectez-vous pour commenter.

Réponse acceptée

Jon
Jon le 8 Juin 2023
Modifié(e) : Jon le 8 Juin 2023
In your case, you only have linear constraints, and bound constraints. So you don't need to use a function to define non-linear constraints.
You can assign your linear constraints ("h(j+1)>h(j) and h(j+1)-h(j)<1.25") as, here I write it for h with only 5 elements as an example (If they are large you could define the matrices below using, for example the diag function, I wrote them out explicitly here so you could easily see what they look like):
% h(j) - h(j+1) <= 0
A1 = [1 -1 0 0 0 ;
0 1 -1 0 0;
0 0 1 -1 0;
0 0 1 -1 0;
0 0 0 1 -1]
b1 = [0;0;0;0;0]
% h(j+1) - h(j) <= 1.25
A2 = [-1 1 0 0 0;
0 -1 1 0 0;
0 0 -1 1 0;
0 0 0 -1 1]
b2 = [1.25;1.25;1.25;1.25;1.25];
% Combine into overall constraint
A = [A1;A2];
b = [b1;b2];
Assign bound constraints (" h(j)<120"
% Bound constraints
lb = -inf
ub = 120
No equality constraints
Aeq = [];
beq = [];
% Call optimization
h = fmincon(Ins, h0, A, b, Aeq, beq, lb, ub);

Plus de réponses (0)

Catégories

En savoir plus sur Surrogate Optimization dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by