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
- Using a global variable for storing the solution.
- 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
Best Answer