Function optimization meeting some conditions
3 views (last 30 days)
Show older comments
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 Comment
Jon
on 8 Jun 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);
Accepted Answer
Jon
on 8 Jun 2023
Edited: Jon
on 8 Jun 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);
0 Comments
More Answers (0)
See Also
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!