title: Adding non linear conditions to matlab optimization package depending on variables not being optimized
body: <p>I am using the global maximization toolbox to maximize the following function:</p>
  
  <pre><code>    function x = NameOfFunction (w1, w2, w3, a, b, c, Structure1, Structure2, Structure3)
  </code></pre>
  
  <p>where I am minimizing <code>x</code> by changing the values of <code>w1</code>, <code>w2</code>, and <code>w3</code>. The remaining parameters are constants and structures containing data. The value of <code>x</code>, as well as the three <code>w</code> variables depend on the data that is fed into the function via the structures.</p>
  
  <p>The function returns <code>x</code> which is the mean of 180 values that are calculated in the process of running <code>NameOfFunction</code>.  </p>
  
  <p>I am wondering how I could add a constraint on the standard deviation of the 180 values.  I am not interested in minimizing both the mean and the standard deviation, but rather to minimize <code>x</code>(the mean), while allowing standard deviation to be no greater than some specific value.  I know how to add constraints to the decision variables (ie. <code>w1</code>, <code>w2</code>, <code>w3</code>), but have no idea how to do so for a value like the standard deviation.</p>
  
  <p>EDIT: More detail, per Werner's suggestion:</p>
  
  <pre><code>%the functions is f(w) rather than f(x)
  
  %constraints:
  Aeq = [1 1 1];
  beq = 1;
  lb = .10 * [1 1 1];
  ub = .8 * [1 1 1];
  
  w = [weight1, weight2, weight3];     
  wstart = randn(3,1);
  
  options = optimset('Algorithm','interior-point');
  
  % function handle for the objective function (note that variables 
  % aa through hh are additional parameters that the solver does not modify): 
  h = @(w)NameOfFunction(w(1),w(2),w(3), aa, bb, cc, dd, ee, ff, gg, hh);
  
  % problem structure:
  problem = createOptimProblem('fmincon','x0',wstart,'objective',h,...'
  'Aeq',Aeq,'beq',beq,'options',options,'lb',lb,'ub',ub);
  
  gs = GlobalSearch;
  run(gs,problem)
  </code></pre>
  
  <p>I'm running a <code>GlobalSearch</code> using <code>fmincon</code>.  </p>
  
  <hr>
  
  <p>7/16/2013, After implementing <code>nonlcon</code> I was able to achieve what I tried to do.  (I have a follow-up question, which I put on the bottom of this post).  Here's what I did:</p>
  
  <p>I added another function (<code>StdConstraintFunction</code>) as discussed.  So now I have the following:</p>
  
  <pre><code>stdMax = 0.01;
  h = @(w)NameOfFunction(w(1),w(2),w(3),aa, bb, cc, dd, ee, ff, gg);
  StdConstraint = @(w)StdConstraintFunction(w(1),w(2),w(3),aa, bb, cc, dd, ee, ff, gg,stdMax);
  </code></pre>
  
  <p>where <code>StdConstraintFunction</code> is a modified version of <code>NameOfFunction</code> that calculates the standard deviation rather than the mean.  </p>
  
  <p>The last line in the two functions is the only thing that is different in the body of the functions.  </p>
  
  <p>In <code>NameOfFunction</code>, the last line is:</p>
  
  <pre><code>ReturnVariable = -1 * (nanmedian([vect1]));
  %note: I added the -1 multiplication to search for the maximum rather than minimum
  </code></pre>
  
  <p>The last line in <code>StdConstraintFunction</code> is:</p>
  
  <pre><code>ReturnVariable = (std([vect1]) - stdMax);
  ceq = []; 
  %ceq is a required variable that is supposed to return the equality non-linear
  %constraint; here it is blank because I don't have one.  The optimization 
  %would produce an error if I exclude it
  </code></pre>
  
  <p>and my problem setup is:</p>
  
  <pre><code>problem = createOptimProblem('fmincon','x0',xstart,'objective',h,'Aeq',Aeq,'beq',beq,'options',options,'lb',lb,'ub',ub,'nonlcon',StdConstraint);
  </code></pre>
  
  <p>@Werner: If you want to post this as the answer to the question, I will gladly accept it as the official answer.  Thanks so much for all your help!</p>