Question regarding passing input arguments in fmincon
Show older comments
Hi All,
I am solving an optimization problem.There are no equality constraints in my model. I want to pass ODEs as constraint to fmincon using,non-linear constraint, nlcon argument .
The function model contains odes
function dz = model(t, z, c)
dz(1) = ..
dz(2) = ..
I'd like to ask how the ode intergrator has to be called in the place of nlcon in fmincon(@objective,p0,A,b,Aeq,beq,lb,ub,nlcon).
%p0 = Initial Value of Parameters
A = [];
b = [];
Aeq = [];
beq = [];
nlcon = ode45(@(t,z)model(t,z,x), tSpan, z0); % Is this correct?
p = fmincon(@objective,p0,A,b,Aeq,beq,lb,ub,nlcon);
Since the ode function is already called in nlcon , is it required to call the function model again inside the objective function?
function cost = objective(c,time_exp,expZ,tSpan,z0)
sol = ode45(@(t,z)model(t,z,c), tSpan, z0); % Is this step required?
ModelZ = deval(sol, time_exp);
cost = ModelZ-expZ;
Any help would be highly appreciated.
14 Comments
Torsten
on 23 Apr 2019
Star Strider's answer should show you how to solve parameter fitting problems involving ODEs:
https://de.mathworks.com/matlabcentral/answers/43439-monod-kinetics-and-curve-fitting
Deepa Maheshvare
on 23 Apr 2019
Edited: Deepa Maheshvare
on 23 Apr 2019
Torsten
on 23 Apr 2019
Use lsqcurvefit. Here you can also prescribe lower and upper bounds for the variables.It doesn't matter that ModelZ are steady-state values. Just proceed as in the given link.
Deepa Maheshvare
on 23 Apr 2019
Deepa Maheshvare
on 24 Apr 2019
Torsten
on 24 Apr 2019
If you want to restrict the values of the solution at all times to be in a certain range, not just the final values, you will have to use "fmincon" for your problem.
In "objective", return cost = sum((ModelZ-expZ).^2).
In "nlcon", choose an n-dimensional vector of time instants "tspan", solve the ODEs at these time instants and return a 2*n - dimensional constraint vector c with lb(i) <= c(2*i-1), ub(i) >= c(2*i) (i=1,...,n).
Deepa Maheshvare
on 24 Apr 2019
Edited: Deepa Maheshvare
on 24 Apr 2019
function [c,ceq] = nonlincon(x)
tSpan = ...;
z0 = ...;
lb = ...;
ub = ...;
[T,Z] = ode45(@(t,z)model(t,z,x), tSpan, z0);
n = numel(z0);
m = numel(tSpan);
for i = 1:n
for j = 1:m
c(2*((i-1)*m + j)-1) = Z(j,i) - ub(i);
c(2*((i-1)*m + j)) = lb(i) - Z(j,i);
end
end
end
If you set tSpan(end) to the value you also use in the objective function, you can call "fmincon" without setting lb and ub because "nonlincon" already accounts for these bounds at tSpan(end).
Deepa Maheshvare
on 24 Apr 2019
Edited: Deepa Maheshvare
on 24 Apr 2019
Deepa Maheshvare
on 25 Apr 2019
Edited: Deepa Maheshvare
on 25 Apr 2019
The bounds lb and ub refered to in "nonlincon" do not store bounds for the parameters, but for the solution vector of your ODE (you wrote you wanted to restrict the values of the solution at all times to be in a certain range). If you want to use bounds for the parameters as well, you can do this directly in the call to "fmincon".
Note that lb, ub are of the same size as z0 in "nonlincon", not of the same size as the parameter vector x.
Note also that you should allocate c as
c = zeros(2*n*m,1);
before entering the double loop.
And set
ceq = [];
at the end of the function.
Deepa Maheshvare
on 26 Apr 2019
Edited: Deepa Maheshvare
on 26 Apr 2019
Torsten
on 26 Apr 2019
Sorry, but I don't have experience with the SimBiology toolbox.
Answers (1)
Alan Weiss
on 23 Apr 2019
0 votes
I am not sure that I understand what you mean by "I want to pass ODEs as a constraint to fmincon." What about the ODE solution is a constraint? Do you want the minimum value of the solution to be above zero? Do you want the ODE solution to lie in a certain box? Once you explicitly define what you mean by the ODE solution is a "constraint," then I think that the answer to your question will be obvious. You will solve the ODE, get sol, and then write the constraint in terms of sol, maybe fun(deval(sol,tspan)) for some appropriate function fun and set of times tspan.
Alan Weiss
MATLAB mathematical toolbox documentation
4 Comments
Deepa Maheshvare
on 23 Apr 2019
Alan Weiss
on 23 Apr 2019
I still do not understand precisely what you are trying to do, so cannot give you a very detailed answer.
If you want your steady-state values to be in a certain range, then you probably want to restrict deval(sol,lasttime), where lasttime is the final time. But if you just want to restrict the steady-state, then probably you shouldn't solve an ODE anyway, just the steady-state equations, which would be an fsolve call, finding the location where dz = zeros(size(z0)).
Maybe you want to restrict the values of the solution at all times to be in a certain range, not just the final values. In that case, you would set constraints on deval(sol,tspan), where tspan is the set of times that you want to restrict.
Maybe you are trying to fit your ODE solution to some observed (measured) values as a function of time. In that case, follow the procedures in this example.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
Deepa Maheshvare
on 23 Apr 2019
Alan Weiss
on 23 Apr 2019
Sorry, I am unable to understand what you are trying to do, so this will be my last reply. I suggest that you write out equations or inequalities describing your constraint. Seriously, write them down. Then, given those equations or inequalities, you can write a corresponding nonlinear constraint function in fmincon syntax, restricting the result by using deval on the solution at relevant times. If you don't understand what I am suggesting, sorry, maybe someone else can help.
Alan Weiss
MATLAB mathematical toolbox documentation
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!