MATLAB Answers

How to add a restriction in fmincon with external parameters and a variable state of ODE?

7 ビュー (過去 30 日間)
Hugo Mendonça
Hugo Mendonça 2017 年 10 月 17 日
コメント済み: Walter Roberson 2017 年 10 月 17 日
Hi, folkes!
The main program:
ab0 = [1; 1]; % initial guess
A = [ 1 0; % a<100
-1 0; % a>0
0 1; % b<22
0 -1];% b>0
b = [100*(1-eps); % a<100
0-eps; % a>0
22*(1-eps); % b<22
0-eps]; % b>0
ab = fmincon(@objectiveFunction, ab0, A, b);
a = ab(1);
b = ab(2);
The derivate function:
function df = derivative(x, f, ab)
a = ab(1);
b = ab(2);
df = zeros(3,1);
df(3)= 5*a/b*f(1)+(f(2)+f(3));
end % end function, derivative
and, finally, the objetive function:
function cost = objectiveFunction(ab)
f0 = [1;1;1];
[~, f] = ode113(@(t,f) derivative(t, f, ab), [0 1], f0);
cost = f(2, end) + f(1, end) - 0.576;
end % end function, objectiveFunction
I see this program calculates the values of a and b that minimise cost subject to some restrictions.
The question is: how could I add a new restriction condition related to a state variable f?
I appreciate your help! Greetings!

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 10 月 17 日
For linear inequality restrictions, you would add to A and b. For linear equality restrictions, you would add new parameters, Aeq and beq after A and b. For upper and lower bounds, you would add lb and ub parameters after Aeq and beq (leaving Aeq and beq as [] if you have no equality constraints).
For non-linear restrictions, you would add a function or anonymous function that took t and y as input, and returned the nonlinear inequality "values are in range" conditions as the first output, and the nonlinear equality "values are in range" conditions as the second output (both must be returned, return [] if need be), and you would put those after lb and ub (putting in [] for those if you need to.) If you have nonlinear constraints that you need extra values to calculate, then parameterize the function.
  2 件のコメント
Walter Roberson
Walter Roberson 2017 年 10 月 17 日
If a derivative being calculated through an ode*() call needs to have restrictions put on it, then the way to do that is to add an event function to the ode options. See


Community Treasure Hunt

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

Start Hunting!

Translated by