Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Differentiation in Script Matlab

  1. Aug 8, 2011 #1
    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!
  2. jcsd
  3. Aug 8, 2011 #2


    User Avatar
    Science Advisor

    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).
  4. Aug 8, 2011 #3
    thanks! :)
  5. Aug 8, 2011 #4


    User Avatar
    Science Advisor

    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):
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - Differentiation Script Matlab Date
Mathematica Rescaling equations in Mathematica May 5, 2017
Matlab Is MATLAB better for numerical simulation Jan 16, 2017
Matlab Numerical Differentiation Mar 19, 2016
Matlab script for central difference Feb 5, 2015