title: Plotting the function value evolution during all iterations using lsqnonlin
body: <p>I am using <code>lsqnonlin</code> as my optimization routine. I need to plot the cost function at each iteration whilst showing all previous values. So I want to show something like <a href="https://www.mathworks.com/help/matlab/math/plot-functions.html" rel="nofollow noreferrer">this</a>:</p>
  
  <p><a href="https://i.stack.imgur.com/CTLbV.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CTLbV.jpg" alt="enter image description here"></a></p>
  
  <p>However, using <code>lsqnonlin</code>, I was only able to plot the value of the cost function at the current iteration only. using these options:</p>
  
  <pre><code>options            = optimset('TolFun', 1e-5, 'TolX',1e-5, 'MaxFunEvals', 10000, 'PlotFcns', @optimplotfval,'Display','iter')
  </code></pre>
  
  <p>Is there a way to set the options of the <code>lsqnonlin</code> such that I get something similar to the above shown figure?</p>