50 views (last 30 days)

Show older comments

I have two questionw about the fmincon function in matlab. I'm using it for a large optimization. The objective function takes about 20sec per call.

The first question is: I calculate a lot of data in the objective function, which I also need in the constraint function. Is there a way to pass this data from the objective function to the constraints function. I know that it is possible to transfer data from the main function to the constraint function and from the main function to the objective function by using function handles. I cannot figure out how to transfer data between the objective and constraint function, however. Right now I use a quick fix; I save the data created in the objective function to a .mat file and then load it again in the constraint function. I prefer a neater solution though.

The second question is also about variable transfer. My optimization features 30 variables. I use an active-line approach, which means that the objective function is called 31 times for each iteration. I would like to have a waitbar which displays the status of each iteration. Therefore I'd like to have access to the amount of iterations and function calls in a separate function. A bit like the OutputFcn, but instead one that is called after each function call. A second solution would be to have the number of iterations/function calls available within the objective function.

Any help would be greatly appreciated.

Walter Roberson
on 14 Jun 2011

Your objective function has two outputs, but objective functions for fmincon must have a single scalar output.

Your objective function is not being created correctly.

Your constraint function must accept a vector of x values as its only input.

Your constraint function is not being created correctly.

Variables will never be considered to be nested in a function if they appear as an input or output argument.

Any variables that you want to share, you have to define in the outer scope before you define the functions that will use them.

Putting these together:

function [] = objectiveconstraints(previous_variables)

objective=@(x) obj(previous_variables,x);

constraint=@con;

aircraft = []; %must initialize shared variables

function f = obj(previous_variables,x)

aircraft = calculations(previous_variables,x)

f = aircraft.objectivevalue;

end

function [c,ceq] = con(x)

%non linear inequalities at x

c = aircraft.c;

% Compute nonlinear equalities at x.

ceq = 0;

end

%Define problem

problem.x0 = x0;

%Define Objective function

problem.objective = objective;

%Define constraint function

problem.nonlcon = constraint;

%Perform optimization

[x,fval] = fmincon(problem);

end

Maximilian Hoffmann
on 13 Jul 2017

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

Start Hunting!