title: Applying fminunc to costFunction for Logistic Regression
body: <p>I have met a problem when practicing coding logistic regression for machine learning course using a Mac version of Matlab_R2016a.</p>
  
  <p>First,this is my code of costFunction which works fine with returning the cost and the gradient:</p>
  
  <pre><code>function [J,grad] = costFunction(X, y, theta)
  
  
      % Initialize some useful values
      h=sigmoid(X*theta);
      m = length(y); % number of training examples  
  
      grad = m^(-1) * ((h-y)'*X)'; 
      J=sum(-y.*log(h)-(1-y).*log(1-h))/m;
  
  end
  </code></pre>
  
  <p>And this is the sigmoid function:</p>
  
  <pre><code>function h=sigmoid(z)
  
  h = (1 + exp(-1 *z)).^(-1); 
  
  end
  </code></pre>
  
  <p>Finally, I applied costFunction to fminunc with the form like this in the following picture(<a href="https://i.stack.imgur.com/fF6g2.png" rel="nofollow noreferrer">enter image description here</a>). But, the strange thing was that it said my Inner matrix dimensions of X*theta in costFunction was wrong(It was fine before). I appreciate some solutions or ideas from you.</p>