title: Fitting a curve in MATLAB using lsqcurvefit
body: <p>I'm trying to fit data with MATLAB using the lsqcurvefit function, but I'm having some trouble.
  MATLAB gives me the following message.</p>
  
  <pre><code>Local minimum possible.
  
  lsqcurvefit stopped because the size of the current step is less than
  the selected value of the step size tolerance.
  
  &lt;stopping criteria details&gt;
  
  Optimization stopped because the norm of the current step, 5.578610e-021,
  is less than options.TolX = 1.000000e-020.
  
  Optimization Metric                                 Options
  norm(step) =  5.58e-021                      TolX =  1e-020 (selected)
  </code></pre>
  
  <p>I've tried changing TolX and TolFun but the only thing that has changed, is the program now takes ages to finish.</p>
  
  <p>The function I'm trying to fit is this:</p>
  
  <pre><code>function P = dIdV(delta, E, T)
  
  %delta in eV
  %E in eV
  %T in K
  
  gamma = 0.00001;
  
  %IV curve
  arr = zeros(size(E));
  A = -E(length(E))*10:E(length(E))/750:E(length(E))*10;
  
  for i=1:length(E)
      arr(i) = trapz(A, (FermiDirac(-E(i)+A, T) - FermiDirac(A,T)) .* SCDOS(-E(i)+A, delta, gamma));
  end
  
  %derivative
  B = zeros(size(E));
  points = 49;
  for i=1:length(E)-points
      p = polyfit(E(i:i+points), arr(i:i+points), 1);
      B(floor(points/2)+i-1) = p(1);
  end
  
  %vector size back to original
  for k=0:ceil(points/2)
      B(k+1) = B(floor(points/2));
      B(length(E)-k) = B(length(E)-ceil(points/2)-1);
  end
  
  P = B
  </code></pre>
  
  <p>FermiDirac is</p>
  
  <pre><code>function P=FermiDirac(E,T)
  
  %E in eV
  %T in Kelvin
  
  kb=8.617343*10^(-5);
  
  P=1./(1+exp(E./(kb.*T)));
  </code></pre>
  
  <p>SCDOS is</p>
  
  <pre><code>function N=SCDOS(E,delta,gamma)
  
  %E in eV
  %delta in eV
  %gamma undefined
  
  N=abs(real((E-1i*gamma)./sqrt((E-1i*gamma).^2-delta^2)));
  </code></pre>
  
  <p>What I'm calculating is the dI/dV curve for a superconductor at some temperature T. I'm supposed to get a value for the energy gap (delta) through fitting.</p>
  
  <p>I would post my data here, but it's 10000 points long, so I'm not sure how to post it. I've tried filtering my data to smooth the curve, but to no avail. I've also tried using different intervals.</p>
  
  <p>Any suggestions on how to make this work are welcome. Better ways to fit are also welcome.</p>
  
  <p>EDIT: <a href="http://i.imgur.com/tpzPfZk.png" rel="nofollow">Here's a graph</a> of the data. The blue line is the derivative of the data as calculated in the function above, the red line is the filtered data and the green line is the theoretical curve. I'm trying to fit the red one to the green one.</p>