MATLAB Answers

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

27 views (last 30 days)
Hugo Mendonça on 17 Oct 2017
Commented: Walter Roberson 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?
I appreciate your help! Greetings!
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

Walter Roberson 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.
##### 2 CommentsShowHide 1 older comment
Walter Roberson 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

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!