title: How do I find the derivative of a function in Octave?
body: <p><strong>Inputs:</strong></p>
  
  <p><code>Xf</code> = and array that holds the x-values of the points</p>
  
  <p><code>Yf</code> = an array that holds the y-values of the points method = 2-point forward difference, 2-point backward difference, 3-point central difference, 5-point central difference</p>
  
  <p><strong>Outputs:</strong></p>
  
  <p><code>X</code> = the array that contains the valid x-values where the method chosen can actually be used (for example, you cannot use the forward difference method at the upper bound of the <code>Xf</code> array because there is no value after it)</p>
  
  <p><code>DF</code> = the derivatives at those points</p>
  
  <p>I need to give a script a set of points and then calculate the derivatives at those points using 4 different methods <strong>without using a built-in derivative function like</strong> <code>diff</code>. I'd like some assistance in coding one of them and then I think I should be able to figure out how to do the rest.</p>
  
  <p><a href="http://imgur.com/vMgiMik" rel="noreferrer">2-point forward difference</a></p>
  
  <p>My attempt:</p>
  
  <pre class="lang-matlab prettyprint-override"><code>[a, minidx] = min(Xf);
  [b, maxidx] = max(Xf);
  n = 10;
  h = (b-a)/n;
  f = (x .^3) .* e.^(-x) .* cos(x);
  
  If method = "forward" #Input by user
  
      X = [min(Xf), Xf(maxidx-1)];
      for k = min(Xf):n # not sure if this is the right iteration range...
  
          f(1) = f(x-2*h) + 8*f(x +h);
          f(2) = 8*f(x-h) + f(x+2*h);
          DF = (f1-f2)/(12*h);
  
      endfor
  endif
  </code></pre>