title: How to get derivative of a function in MATLAB?
body: <p>In real life it is piece of cake, but how you get derivative of a quadratic or cubic function in matlab?</p>
  
  <p>For example, <code>A*x^3 + B*x^2 + C*x + D</code> will be <code>3*Ax^2 + 2*b*x + C</code></p>
  
  <p>I want to get this in matlab, but I can't figure out how :(</p>
  
  <p>for example I tried this code but I get stupid result (maybe I am the one who should be blamed!):</p>
  
  <pre><code>&gt;&gt; x = [6 3 2 1]
  
  x =
  
       6     3     2     1
  
  &gt;&gt; xPrime = diff(x)
  
  xPrime =
  
      -3    -1    -1
  </code></pre>
  
  <p>Normally it should give [18 6 2] ??
  Also I Want to know how yo do this for range of numbers. For example I want derivative of each point for the example above for <code>n = linspace(0,10,1000)</code></p>
  
  <p><strong>update</strong>
  of course I can do this manually, but I really like to know how to do it with matlab itself.</p>
  
  <p>What I am doing now is getting the tangent line of the above example and I am doing like this and it works:</p>
  
  <pre><code>x = linspace(0,10,1000);
  y=A*x.^3+B*x.^2+C*x + D;
  plot(x,y);
  hold on;
  slop=3*A.*(Location^2)+2*B.*Location+C;
  b=(A.*Location.^3)+(B.*Location.^2)+(C.*Location)+D;
  y2=slop*(x-Location)+b;
  plot(x,y2,'--r');
  legend('Graph of the function','Tangent Line');
  hold off;
  </code></pre>
  
  <p>What I mean is what do I should use instead of hand calculated derivative in this line:</p>
  
  <pre><code>slop=3*A.*(Location^2)+2*B.*Location+C;
  </code></pre>
  
  <p>Thanks!</p>