title: Matlab - Properly setting up fmincon when objective and nonlinear constriants need parameters
body: <p>I am trying to follow the advice given here</p>
  
  <p><a href="http://www.mathworks.com/help/toolbox/optim/ug/brhkghv-7.html" rel="nofollow">http://www.mathworks.com/help/toolbox/optim/ug/brhkghv-7.html</a></p>
  
  <p>but I am having trouble.  I have a complicated objective to minimize subject to complicated nonlinear constraints, all of which are functions of my 3 choice variables, and a wide variety of parameters. My script that starts everything looks like this:</p>
  
  <pre><code>% define a bunch of parameters
  gamma= .2;
  beta = .3; %
  x0=[.5 .5 .5];
  %etc
  solution = nested_minimization_program(x0,gamma,beta)
  </code></pre>
  
  <p>Nested_minimization_program looks like this:</p>
  
  <pre><code>function out = nest_minimization-program(x0,gamma,beta)
  options = optimset('GradObj','on');
  out = fmincon(@objective,x0,[],[],[],[],[0 0 0],[1 1 1],@nonlin,options)
   function [obj obj_gradient] = objective(x)
     [obj obj_gradient] = complicated_objective(x,gamma,beta);
   end
   function [ineq_constriant eq_constraint] = nonlin(x)
    [ineq_constriant eq_constraint] = complicated_constaints(x,beta,gamma)
   end
  end
  </code></pre>
  
  <p>Complicated_objective is a file that returns the value of the objective for its first argument, and the value of anlaytical gradient for its second.  Complicated_constaints returns a vector of nonlinear inequality constraints for its first argument, and a vector of nonlinear equality constraints for its second.</p>
  
  <p>The reason to do this is so that I can use the @objective and @nonlin syntax for fmincon; objective and nonlin are only functions of x, not of the parameters, because they are subfunctions of a function that has been passed the parameters already.  I believe this is the form I should use in order to pass the gradient and the nonlinear constraints on to fmincon.  My problem is that when I run this code, I get the following error</p>
  
  <blockquote>
    <p>Warning: Trust-region-reflective algorithm does not solve this type of problem,
    using active-set algorithm. You could also try the interior-point or sqp
    algorithms: set the Algorithm option to 'interior-point' or 'sqp' and rerun. For
    more help, see Choosing the Algorithm in the documentation. </p>
  </blockquote>
  
  <p>IE, for some reason fmincon is leaving the Trust-region-reflective algorithm and going to active set, which does not make use of my analytical gradient.  The requirements for fmincon to use analytical gradients is, according to <a href="http://www.mathworks.com/help/toolbox/optim/ug/brhkghv-3.html" rel="nofollow">http://www.mathworks.com/help/toolbox/optim/ug/brhkghv-3.html</a>,</p>
  
  <blockquote>
    <p>Write code that returns:
       The objective function (scalar) as the first output</p>
    
    <p>The gradient (vector) as the second output</p>
    
    <p>Set the GradObj option to 'on' with optimset. </p>
  </blockquote>
  
  <p>objective returns a scalar value of the objective and a gradient as required, Gradobj is turned on, so I don't see my problem.</p>