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

Problem with Matlab

  1. Sep 4, 2015 #1
    Hi, I have a problem with my matlab code. The error is: "Function 'diff' is not supported for class 'function_handle'."
    I have installed the toolbox Symbolic Math Toolbox after the installation of Matlab, but I think that it works, so my code is this:

    Code (Matlab M):


    syms p;
    x=@(p) t^(-alpha-1)*exp(-((t^2)/2)-p*t);
    psi=@(p) exp(eta((p-mu)^2/2*(sigma)^2))*D*(((p-mu)/sigma)*sqrt(2*eta));
    Theta=@(p) (p-c)*diff(psi)-psi;

    Can you see the mistake? Thank you for your help. Bye.
  2. jcsd
  3. Sep 4, 2015 #2


    Staff: Mentor

    Here's one comment I found that said to replace the diff argument with something like psi(p) ? The original post said diff(f) --> diff(f(x)

    Here's the MATLAB docs on diff where they mention that the diff argument must be a vector not a function:

  4. Sep 4, 2015 #3


    User Avatar
    Gold Member

    The diff function has different uses in MATLAB vs. Symbolic Math Toolbox. In MATLAB, diff calculates differences between the elements in a vector or matrix. You can then use those numeric differences to find a numerical approximation to the derivative, assuming the values in the vector or matrix represent function values for some unknown function.

    In Symbolic Math Toolbox, diff differentiates a symbolic expression to obtain the derivative. This necessarily requires that you input a valid symbolic expression.

    So the problem you're encountering is due to the fact that you're combining symbolic and numeric variables in a function handle expression, then expecting diff to know what to do with it. When diff sees a function handle input, it calls the MATLAB version of the function, which is not defined for function handles, hence the error you receive.

    The below code runs but you'll have to double check it because you had a typo in the definition of psi. I made the following changes:

    1. D, psi, and Theta do not need to be function handles. Removing the @(p) from the beginning turns them into symbolic expressions.
    2. x does need to be a function handle because you're feeding it in to the integral function.
    3. fzero wants a function handle input. The function matlabFunction() turns a symbolic expression into a function handle, so you just need to convert Theta and then fzero works nicely.
    4. In your definition of psi, you have a symbolic expression inside parentheses for eta(),
    Code (Text):
    This produces an error because eta is equal to 0.2, and the indexing doesn't make sense in that case. In my code below I added eta*() to multiply, but you should double check what this expression should actually be.

    Code (Text):


    syms p;
    x = @(p) t^(-alpha-1)*exp(-((t^2)/2)-p*t);
    D = (exp(-(p^2)/4)/gamma(alpha))*integral(x,0,Inf);
    psi = exp(eta*((p-mu)^2/2*(sigma)^2))*D*(((p-mu)/sigma)*sqrt(2*eta));
    Theta = (p-c)*diff(psi)-psi;
    Hope this helps.
  5. Sep 4, 2015 #4
    Thanks a lot, very clear explenation!! And thank you for the remark of eta, that was a multiplication :)
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Problem with Matlab
  1. Matlab problem (Replies: 1)

  2. Matlab problem (Replies: 4)

  3. Matlab problem (Replies: 0)

  4. MATLAB problem (Replies: 5)

  5. MATLAB problem (Replies: 1)