Function optimization meeting some conditions

4 ビュー (過去 30 日間)
Jon Bilbao
Jon Bilbao 2023 年 6 月 8 日
編集済み: Jon 2023 年 6 月 8 日
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 件のコメント
Jon
Jon 2023 年 6 月 8 日
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);

サインインしてコメントする。

採用された回答

Jon
Jon 2023 年 6 月 8 日
編集済み: Jon 2023 年 6 月 8 日
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);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurrogate Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by