title: MatLab - gradient command
body: <p>I am trying to learn various functions and commands in MatLab.  I have a question regarding the <code>gradient</code> command.</p>
  
  <p>Say I define the following:</p>
  
  <pre><code>x = 0:1:10;
  
  f = @(x) x.^2 + 2*x -1;
  
  h = gradient(f(x))
  </code></pre>
  
  <p>This then gives me the following vector:</p>
  
  <pre><code>h =  3     4     6     8    10    12    14    16    18    20    21
  </code></pre>
  
  <p>I see that the values are correct when x is between 1 and 9, but this is incorrect for x = 0 and x = 10.  When x = 0, the gradient should be 2, and when x = 10, the gradient should be 22.  So why does MatLab give erroneous answers for these two input values?</p>
  
  <p>If anyone could explain this to me I would greatly appreciate it!</p>