title: Function works with lsqcurvefit but not nlinfit
body: <p>I'm attempting to get a nonlinear least squares fit for the following function:</p>
  
  <pre><code>Nloc = 250;
  
  d = 1 / Nloc;
  
  m = 0.5; %Initial guess
  
  ncmfun = @(m, p) arrayfun(@(p) betainc(d, Nloc*m .* p, Nloc*m .* (1 - p), 'upper'), p);
  </code></pre>
  
  <p>Where m is the parameter being fit, Nloc and d are constants, and p and freq are vectors of positive real numbers and of the same length (I triple checked). When I use lsqcurvefit everything works fine:</p>
  
  <pre><code>[mfit,resnorm,]  = lsqcurvefit(ncmfun, m, p, freq, 0, 1)
  </code></pre>
  
  <p>Also, if I use any m to evaluate the function, everything works fine as well. However when I use nlinfit:</p>
  
  <pre><code>[mfit,R,Jac,CovB,MSE,ErrorModelInfo] = nlinfit(p, freq, ncmfun, m)
  </code></pre>
  
  <p>I get the following error(s):</p>
  
  <pre><code>Error using betainc
  
  Z must be real and non-negative.
  
  Error in @(p)betainc(d,Nloc*m.*p,Nloc*m.*(1-p),'upper')
  
  
  Error in
  @(m,p)arrayfun(@(p)betainc(d,Nloc*m.*p,Nloc*m.*(1-p),'upper'),p)
  
  
  Error in nlinfit&gt;@(b,x)w.*model(b,x) (line 206)
          modelw = @(b,x) w.*model(b,x);
  
  Error in nlinfit&gt;LMfit (line 486)
      yfit = model(beta,X);
  
  Error in nlinfit (line 207)
          [beta,J,~,cause,fullr] = LMfit(X,yw,
          modelw,beta,options,verbose,maxiter);
  
  Error in Sloan_NCM_Parameterize_Nm (line 37)
  [mfit,R,Jac,CovB,MSE,ErrorModelInfo] = nlinfit(p, freq,
  ncmfun, m);
  </code></pre>
  
  <p>What's especially frustrating was this same script was working a couple weeks ago. I then tried to use it again and it no longer works. I tried to go through and see if I accidentally changed something and didn't remember, but I can't find any errors. Furthermore, I'm confused as to why lsqcurvefit works but not nlinfit. I'd like to use nlinfit because it provides me with more statistical information about the error.</p>
  
  <p>Thanks in advance for any help you can provide!</p>