title: Linear Regression Code
body: <p>I am taking <em>Andrew Ng class</em> on <em>Machine Learning</em> and implementing linear regression algorithm.</p>
  
  <p>What is wrong with my code? </p>
  
  <pre><code>function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
  m = length(y); 
  J_history = zeros(num_iters, 1);
  h = (X*theta)
  for iter = 1:num_iters
      theta(1,1) = theta(1,1)-(alpha/m)*sum((h-y).*X(:,1));
      theta(2,1) = theta(2,1)-(alpha/m)*sum((h-y).*X(:,2));  
      J_history(iter) = computeCost(X, y, theta);
  end
  end
  </code></pre>
  
  <p>Cost Function is given as:</p>
  
  <pre><code>function J = computeCost(X, y, theta)
  m = length(y); 
  h = (X*theta)
  J = (1/(2*m))*sum((h-y).^2)
  end
  </code></pre>
  
  <p>The value of <code>J_history</code> keeps increasing. It is giving very abnormal (large value) i.e. about 1000 times more than it should.</p>
  
  <p><a href="https://i.stack.imgur.com/H0Itw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H0Itw.png" alt="image"></a></p>