Optimization: solving ODE in objective function, how to pass solution to nonlinear constraint function?

Dear all,
More specifically, I have a model struct with all model parameters and a handle on the derivative function. I created "wrappers" that pass this struct in addition to the parameters for optimization to both the objective function and the constraint function:
wrapper = @(x) objfunc( x, model ) ;
wrapperConstraint = @(x) constrfunc( x, model ) ;
x0 = ...
lb = ...
ub = ...
... = fmincon( wrapper, x0, [], [], [], [], lb, ub, wrapperConstraint, ... ) ;
when the objective function is called, it solves a system of ODEs, but there is no direct way to pass it to the constraint function. The only two options that I see are
  1. Using a global variable for storing the solution.
  2. OOP: e.g. defining model not as a struct but as a handle class, and storing the solution in a property. This way both the objective function and the constraint function would access the same object.
Is there a better/cleaner option? What is the "correct/usual" way of doing this?
Thank you,
Karin

 Accepted Answer

2 Comments

For some more explicit help of what Sean suggests, see Objective and Nonlinear Constraints in the Same Function, and maybe even Optimize an ODE in Parallel.
Alan Weiss
MATLAB mathematical toolbox documentation
Thank you both Sean and Alan. I am facing a problem with UseParallel enabled, I guess because multiple workers access the same variable (from the containing function, that corresponds to xLast in the doc example); I will work on this tomorrow and come back to you if I can't figure it out.

Sign in to comment.

More Answers (0)

Asked:

on 6 Sep 2016

Edited:

on 6 Sep 2016

Community Treasure Hunt

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

Start Hunting!