# Differentiation in Script Matlab

1. Aug 8, 2011

### c299792458

How might I make my script perform differentiation on a user-defined function? I also want the function and its derivative (in the form of a function) to be applied throughout the script, say in the forms of f and f' respectively, which I believe requires str2func.

Example: The user is prompted for an f(x). He enters x^3. Then both x^3 and 3*x^2 are applied in the place of f and f' throughout the script.

2. Aug 8, 2011

### uart

See my answer to your related question in the Math Software sub-forum.

Unless you are using the "symbolic" features, the only options you have for the derivative is to either make the user enter both the function and the derivative or else you can approximate the derivative numerically as (f(x+h) - f(x))/h for some suitably small h.

BTW f' is not a suitable name for a variable in matlab. You'll have to choose something else to denote the derivative (df or fd for example).

3. Aug 8, 2011

thanks! :)

4. Aug 8, 2011

### MATLABdude

You could write yourself a really, really basic differentiation function / script in MATLAB, but you'd have to do tokenizing to break down the entry and then apply the appropriate differentiation pattern.

But if you're looking to do a lot of symbolic math, you're better off with an actual symbolic math package (e.g. Mathematica, Maple, or the Symbolic Math Toolbox for MATLAB). There's also a few open-source / freeware ones available (like Maxima or Axiom, both with various add-on GUIs):
http://en.wikipedia.org/wiki/Comparison_of_computer_algebra_systems