How to pass iteration information to fmincon's nonlcon function?

1 view (last 30 days)
Dear all,
My code uses fmincon to minimize the distance from (snap) a set of points to a surface and find snapped coordinates:
[coord_snapped,fval,exitflag,output] = fmincon(efun,coord0,[],[],[],[],[],[],cfun,options);
The nonlinear constraint (nonlcon) function is specified as anonymous function:
cfun=@(coord_snapped)surface_constraint_ref(coord_snapped,surf,coord_projected_nn);
where surface_constraint_ref function does something.
What I need is to do something for the first iteration (Iter=0), and something else for all other iterations; one can write surface_constraint_ref in the form:
if Iter==0
something;
else
something else;
end
Iter=Iter+1;
and passing Iter as both input and output argument to surface_constraint_ref. This does not work. My problem is how to pass iteration information to cfun?
I tried: 1.
Iter=0; %initial iteration
cfun=@(coord_snapped)surface_constraint_ref(coord_snapped,surf,coord_projected_nn,Iter);
but cfun does not switch from something to something else, and Iter stay 0 on debug
2.
cfun=@(coord_snapped,Iter)surface_constraint_ref(coord_snapped,surf,coord_projected_nn);
3.
cfun=@(coord_snapped,output)surface_constraint_ref(coord_snapped,surf,coord_projected_nn);
and in surface_constraint_ref
if output.iterations==0
something;
else
something else;
end
With 2 and 3, I get an error of not enough input arguments (2) or field reference attempt of nonstructure array (3), so neither Iter nor output information is available to cfun with the first iteration.
How do I proceed, please help,
Thank you,
Octavian

Answers (0)

Community Treasure Hunt

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

Start Hunting!