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

### Hugo Mendonça (view profile)

on 17 Oct 2017
Latest activity Commented on by Walter Roberson

### Walter Roberson (view profile)

on 17 Oct 2017
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(1)=3*a/b*f(2)*f(1)+16*(f(3)-f(1));
df(2)=-3*a/b*f(2)*f(1)+(f(3)-f(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?

### Tags ### Walter Roberson (view profile)

on 17 Oct 2017

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 http://www.mathworks.com/help/matlab/math/parameterizing-functions.html parameterize the function.

Hugo Mendonça

### Hugo Mendonça (view profile)

on 17 Oct 2017
Thank you, Walter, for the quickly answer.
I understand what you said. The problem is the restriction that I would like to add is, for example:
f(1)<1
So, in this sense, I should add to A. The problem is f(1) is calculated by ODE function and it is not a variable calculate to minimise like ab. At this point I get lost.
Walter Roberson

### Walter Roberson (view profile)

on 17 Oct 2017
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 https://www.mathworks.com/help/matlab/math/ode-event-location.html