# How do I differentiate in MATLAB?

1. Mar 20, 2013

### Urmi Roy

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. Mar 20, 2013

### jhae2.718

3. Mar 22, 2013

### kreil

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)

4. May 10, 2013

### Urmi Roy

Thanks for the info, Kreil! I can't imagine why I didn't see your post in all these days!