Question

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.

Was it helpful?

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

OTHER TIPS

Numerical differentiation is kind of tricky with small data sets. The best way I can think of offhand to do this is to explicitly create the function and then apply it to the data.

>> syms x y
>> y = x^2;
>> dy = diff(y);
>> dy

ans = 
    2*x

>> v = 1:5;
>> f = matlabFunction(dy);
>> f(v)



ans = 
    2 4 6 8 10

You have no symbolic toolbox and you want the symbolic derivative. Here is a suggestion: If you already know the derivative of the function you are trying to apply to your vector (with knowledge of basic calculus), just apply it directly to your vector. For example:

vector = [1 2 3 4 5] % intended function = x^2
result = 2*vector
vector = [1 2 3 4 5] % intended function = sin(x)
result = cos(vector)

Is there any function you are trying to apply which you don't already know the derivative?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top