title: Non linear function parameter estimation - matlab, lsqnonlin, fzero
body: <p>I'm having difficulty with a fitting problem. From the errors that I get I imagine that the boundaries are not defined correctly and I haven't managed to find a solution. Any help would be very much appreciated.</p>
  
  <p>Alternative methods for the solution of the same problem are also accepted.</p>
  
  <h1>Description</h1>
  
  <p>I have to estimate the parameters of a non-linear function of the type:</p>
  
  <pre><code>A*y(x) + B*EXP(C*y(x)) + g(x,D) = 0
  </code></pre>
  
  <p>subjected to the parameters <code>PAR = [A,B,C,D]</code> being within the range </p>
  
  <pre><code>LB &lt; PAR &lt; UB
  </code></pre>
  
  <h1>Code</h1>
  
  <p>To solve the problem I'm using the Matlab functions <code>lsqnonlin</code> and <code>fzero</code>. The simplified code used is reported below.</p>
  
  <p>The problem is divided in four functions:</p>
  
  <ol>
  <li><code>parameterEstimation</code> -         (a wrapper for the lsqnonlin function)</li>
  <li><code>objectiveFunction_lsq</code> -   (the objective function for the param estimation)</li>
  <li><code>yFun</code> -            (the function returing the value of the variable y)</li>
  <li><code>objectiveFunction_zero</code> - (the objective function of the non-linear equation used to calculate y)</li>
  </ol>
  
  <h1>Errors</h1>
  
  <p>Running the code on the data I get the this waring</p>
  
  <blockquote>
    <p>Warning: Length of lower bounds is > length(x); ignoring extra bounds</p>
  </blockquote>
  
  <p>and this error</p>
  
  <blockquote>
    <p>Failure in initial user-supplied objective function evaluation.
    LSQNONLIN cannot continue</p>
  </blockquote>
  
  <p>This makes me to think that the boundaries are not correctly used or not correctly called, but maybe the problem is elsewhere.</p>
  
  <hr>
  
  <pre><code>function Done = parameterEstimation()
      %read inputs
      Xmeas = xlsread('filepath','worksheet','range');
      Ymeas = xlsread('filepath','worksheet','range');
  
      %inital values and boundary conditions
      initialGuess = [1,1,1,1]; %model parameters initial guess
      LB = [0,0,0,0]; %model parameters lower boundaries
      UB = [2,2,2,2]; %model parameters upper boundaries
  
      %parameter estimation
      calcParam = lsqnonlin(@objectiveFunction_lsq_2,initialGuess,LB,UB,[],Xmeas,Ymeas);
      Done = calcParam;
  
  function diff = objectiveFunction_lsq_2(PAR,Xmeas,Ymeas)
      y_calculated = yFun(PAR,Xmeas);
      diff = y_calculated-Ymeas;
  
  function result = yFun(PAR,X)
      y_0 = 2;
      val = fzero(@(y)objfun_y(y,PAR,X),y_0);
      result = val;
  
  function result = objfun_y(y,PAR,X)
      A = PAR(1);
      B = PAR(2);
      A = PAR(3);
      C = PAR(4);
      D = PAR(5);
  
      val = A*y+B*exp(y*C)+g(D,X);
      result = val;
  </code></pre>