Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How do I differentiate in MATLAB?

  1. Mar 20, 2013 #1
    I find that I don't have the symbolic toolbox. Due to this, I'm not being able to differentiate by using the usual 'diff' command. However, just as one can integrate numerically, by using the quad and quadl functions, is there no way to differentiate numerically on MATLAB (like using the ab initio method or something)?
  2. jcsd
  3. Mar 20, 2013 #2


    User Avatar
    Gold Member

  4. Mar 22, 2013 #3


    User Avatar
    Gold Member

    You can use diff to differentiate most functions with an appropriate step size. For example,

    Code (Text):
    h = 0.001;
    x = -pi:h:pi;
    y = sin(x);
    z = diff(y)/h;
    If you plot z, you will see that it is equal to cos(x). Similarly, you can use diff to calculate derivatives this way for other functions.

    Just remember diff returns a vector with 1 less element.

    Alternatively, you could use gradient, which returns a vector of the same size. For 1D problems this is a simple partial derivative. There are additional syntaxes available for higher dimensional problems (see http://www.mathworks.com/help/matlab/ref/gradient.html)
  5. May 10, 2013 #4
    Thanks for the info, Kreil! I can't imagine why I didn't see your post in all these days!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook