Search code examples
matlabcalculus

Doing a derivative in MatLab


As the title suggests, I am wondering how I can find the derivative of a 'function'. I am unsure what to do because I really do not have a function defined, just an array of values. So lets try a simple example:

x = 1:5;
y = x.^2
y =
     1     4     9    16    25

As expected. Now I want to take the derivative. I know that this is 2x. Possibly use the diff function, as if in symbolic toolkit?

diff(y)
ans =
     3     5     7     9

This looks like either 2x +// 1, with length length(y)-1. Is there any way I can compute the derivative of this and get a vector of length(y)?

I do not have the symbolic toolkit.


Solution

  • There is little you can do. This topic has already been discussed ad nauseum at the MathWorks web forums.

    @noah has provided an example of numerical differentiation (ie: differencing) on a finite data set, whereas you are looking for a means of doing symbolic differentiation of continuous-domain-continuous-range function applied to a discrete-domain data set.

    In short, not happening. You're asking how to install a shelf without a hammer, screw driver, or nails. You could always just download and install GNU Octave, a free alternative to MATLAB.


    References.

    1. "Symbolic Math In MATLAB" http://faraday.elec.uow.edu.au/subjects/annual/ECTE313/Symbolic_Maths.pdf
    2. "Symbolic differentiation without symbolic math toolbox" http://www.mathworks.com/matlabcentral/newsreader/view_thread/83397
    3. "GNU Octave - Download" http://www.gnu.org/software/octave/
    4. "Symbolic differentiation with GNU Octave" http://octave.1599824.n4.nabble.com/Examples-for-use-of-symbolic-toolbox-td1601724.html