title: Find minimum of nonlinear system of equations with nonlinear equality and inequality constraints in MATLAB
body: <p>I need to solve this problem better described at the title. The idea is that I have two nonlinear equations in four variables, together with two nonlinear inequality constraints. I have discovered that the function <code>fmincon</code> is probably the best approach, as you can set everything I require in this situation (please let me know otherwise). However, I'm having some doubts at the implementation stage. Below I'm exposing the complete case, I think it's simple enough to be in its real form.</p>
  
  <p>The first thing I did was to define the objective function in a separate file.</p>
  
  <pre><code>function fcns=eqns(x,phi_b,theta_b,l_1,l_2)
  
  fcns=[sin(theta_b)*(x(1)*x(4)-x(2)*x(3))+x(4)*sqrt(x(1)^2+x(2)^2-l_2^2)-x(2)*sqrt(x(3)^2+x(4)^2-l_1^2);
        cos(theta_b)*sin(phi_b)*(x(1)*x(4)-x(2)*x(3))+x(3)*sqrt(x(1)^2+x(2)^2-l_2^2)-x(1)*sqrt(x(3)^2+x(4)^2-l_1^2)];
  </code></pre>
  
  <p>Then the inequality constraints, also in another file.</p>
  
  <pre><code>function [c,ceq]=nlinconst(x,phi_b,theta_b,l_1,l_2)
  
  c=[-x(1)^2-x(2)^2+l_2^2; -x(3)^2-x(4)^2+l_1^2];
  ceq=[];
  </code></pre>
  
  <p>The next step was to actually run it in a script. Below, since the objective function requires extra variables, I defined an anonymous function <code>f</code>. In the next line, I did the same for the constraint (anonymous function). After that, it's pretty self explanatory.</p>
  
  <pre><code>f=@(x)norm(eqns(x,phi_b,theta_b,l_1,l_2));
  f_c=@(x)nlinconst(x,phi_b,theta_b,l_1,l_2);
  
  x_0=[15 14 16 18],
  LB=0.5*[l_2 l_2 l_1 l_1];
  UB=1.5*[l_2 l_2 l_1 l_1];
  
  [res,fval]=fmincon(f,x_0,[],[],[],[],LB,UB,f_c),
  </code></pre>
  
  <p>The first thing to notice is that I had to transform my original objective function by the use of <code>norm</code>, otherwise I'd get a <code>"User supplied objective function must return a scalar value."</code> error message. So, is this the best approach or is there a better way to go around this?</p>
  
  <p>This actually works, but according to my research (one question from stackoverflow actually!) you can guide the optimization procedure if you define an equality constraint from the objective function, which makes sense. I did that through the following code at the constraint file:</p>
  
  <pre><code>ceq=eqns(x,phi_b,theta_b,l_1,l_2);
  </code></pre>
  
  <p>After that, I found out I could use the <code>deal</code> function and define the constraints within the script.</p>
  
  <pre><code>c=@(x)[-x(1)^2-x(2)^2+l_2^2; -x(3)^2-x(4)^2+l_1^2];
  f_c=@(x)deal(c(x),f(x));
  </code></pre>
  
  <p>So, which is the best method to do it? Through the constraint file or with this function?</p>
  
  <p>Additionally, I found in MATLAB's documentation that it is suggested in these cases to set:</p>
  
  <pre><code>f=@(x)0;
  </code></pre>
  
  <p>Since the original objective function is already at the equality constraints. However, the optimization doesn't go beyond the initial guess obviously (the cost value is already 0 for every solution), which makes sense but leaves me wondering why is it suggested at the documentation (last section here: <a href="http://www.mathworks.com/help/optim/ug/nonlinear-systems-with-constraints.html" rel="nofollow">http://www.mathworks.com/help/optim/ug/nonlinear-systems-with-constraints.html</a>).</p>
  
  <p>Any input will be valued, and sorry for the long text, I like to go into detail if you didn't pick up on it yet... Thank you!</p>