MATLAB Differentiation in Script Matlab

AI Thread Summary
To perform differentiation on a user-defined function in MATLAB, users can utilize the `str2func` function to convert string inputs into function handles. If symbolic features are not used, users must either input both the function and its derivative manually or use numerical approximation methods for the derivative. It is important to avoid using "f'" as a variable name in MATLAB, opting instead for alternatives like "df" or "fd." For extensive symbolic mathematics, it is recommended to use dedicated symbolic math software such as Mathematica or the Symbolic Math Toolbox for MATLAB. This approach ensures more efficient and accurate differentiation capabilities.
c299792458
Messages
67
Reaction score
0
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.

Thanks in advance!
 
Physics news on Phys.org
c299792458 said:
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.

Thanks in advance!

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).
 
thanks! :)
 
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
 

Similar threads

Back
Top