title: Exponential fit with the least squares Python
body: <p>I have a very specific task, where I need to find the slope of my exponential function.</p>
  
  <p>I have two arrays, one denoting the wavelength range between 400 and 750 nm, the other the absorption spectrum. x = wavelengths, y = absorption.</p>
  
  <p>My fit function should look something like that:</p>
  
  <p><code>y_mod = np.float(a_440) * np.exp(-S*(x - 440.))</code></p>
  
  <p>where <code>S</code> is the slope and in the image equals 0.016, which should be in the range of S values I should get (+/- 0.003). <code>a_440</code> is the reference absorption at 440 nm, x is the wavelength.</p>
  
  <p>Modelled vs. original plot:</p>
  
  <p><a href="https://i.stack.imgur.com/rQ8OE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rQ8OE.png" alt="enter image description here"></a></p>
  
  <p>I would like to know how to define my function in order to get an <strong>exponential fit</strong> (<strong>not</strong> on log transformed quantities) of it without <strong>guessing beforehand what the S value is</strong>.</p>
  
  <p>What I've tried so far was to define the function in such way:</p>
  
  <pre><code>def func(x, a, b):
      return a * np.exp(-b * (x-440))
  </code></pre>
  
  <p>And it gives pretty nice matches</p>
  
  <p><a href="https://i.stack.imgur.com/aFI2Y.png" rel="nofollow noreferrer">fitted vs original</a>. </p>
  
  <p>What I'm not sure is whether this approach is correct or should I do it differently? 
  How would one use also the least squares or the absolute differences in y approaches for minimization in order to remove the effect of overliers?</p>
  
  <p>Is it possible to also add random noise to the data and recompute the fit?</p>