Function optimization meeting some conditions

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by