title: How to plot a contour graph for a linear regression cost function?
body: <p>I'm following the <a href="https://www.coursera.org/learn/machine-learning/" rel="nofollow noreferrer">machine learning course</a> on Coursera, and one of the lectures provides a contour plot of a cost function for linear regression on one variable:</p>
  
  <p><a href="https://i.stack.imgur.com/WTTor.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WTTor.jpg" alt="enter image description here"></a></p>
  
  <p>Source: <a href="https://www.coursera.org/learn/machine-learning/lecture/nwpe2/cost-function-intuition-ii" rel="nofollow noreferrer">https://www.coursera.org/learn/machine-learning/lecture/nwpe2/cost-function-intuition-ii</a></p>
  
  <p>I thought it would be useful from an educational standpoint to be able to reproduce this chart.  I don't have any octave experience, so I would need step by step instructions that I could paste into the octave command window.</p>
  
  <p>Anyone here able to help with this?</p>
  
  <hr>
  
  <p><strong>Update:</strong></p>
  
  <p>I ended up with the following:</p>
  
  <pre><code>function cost = calc_cost (theta0, theta1)
     x = 1:10;
     y = x.*2;
     cost = arrayfun( @(t0, t1) ( 1/(length(x)) * sum( ((t0 + t1*x) - y).^2 )), theta0, theta1);
  endfunction
  
  [xx, yy] = meshgrid( -3000:50:3000, -3000:50:3000) ;
  zz = calc_cost(xx, yy);
  contour(xx, yy, zz )
  </code></pre>