Error in MATLAB Code: Fixing 'Function 'diff' is Not Supported

  • MATLAB
  • Thread starter FrancescoMi
  • Start date
  • Tags
    Matlab
In summary, The conversation discusses a problem with a MATLAB code that utilizes the diff function. The error is caused by a combination of symbolic and numeric variables in a function handle expression. The solution involves removing function handles from certain expressions and converting symbolic expressions into function handles using the matlabFunction() function. There is also a typo in the definition of psi that needs to be corrected.
  • #1
FrancescoMi
5
0
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:

Matlab:
t=2;
alpha=2;
p_0=20;
c=10;
eta=0.2;
mu=10;
sigma=0.5;

syms p;
x=@(p) t^(-alpha-1)*exp(-((t^2)/2)-p*t);
D=@(p)(exp(-(p^2)/4)/gamma(alpha))*integral(x,0,Inf);
psi=@(p) exp(eta((p-mu)^2/2*(sigma)^2))*D*(((p-mu)/sigma)*sqrt(2*eta));
Theta=@(p) (p-c)*diff(psi)-psi;
fzero(Theta,p_0)

Can you see the mistake? Thank you for your help. Bye.
 
Physics news on Phys.org
  • #3
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 into 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:
eta((p-mu)^2/2*(sigma)^2)
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:
t=2;
alpha=2;
p_0=20;
c=10;
eta=0.2;
mu=10;
sigma=0.5;

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;
fzero(matlabFunction(Theta),p_0)

Hope this helps.
Josh
 
  • Like
Likes jedishrfu
  • #4
kreil said:
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 into 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:
eta((p-mu)^2/2*(sigma)^2)
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:
t=2;
alpha=2;
p_0=20;
c=10;
eta=0.2;
mu=10;
sigma=0.5;

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;
fzero(matlabFunction(Theta),p_0)

Hope this helps.
Josh

Thanks a lot, very clear explenation! And thank you for the remark of eta, that was a multiplication :)
 

What is the meaning of the error message "Function 'diff' is Not Supported" in MATLAB?

The error message "Function 'diff' is Not Supported" in MATLAB indicates that there is an issue with the "diff" function in the code. This function is used to compute the differences between consecutive elements in a vector or matrix. It is possible that the function is not supported in the version of MATLAB being used, or that there is a mistake in the syntax or input arguments.

How can I fix the "Function 'diff' is Not Supported" error in MATLAB?

To fix the error, first check the version of MATLAB being used and make sure that the "diff" function is supported. If it is supported, then carefully review the syntax and input arguments of the "diff" function in the code to ensure that they are correct. It may also be helpful to consult the MATLAB documentation or seek assistance from other users or experts.

Why does the "Function 'diff' is Not Supported" error only occur for certain inputs?

The "diff" function in MATLAB has specific requirements for its input arguments. It may only be used for vectors or matrices with numeric or logical values. If the input arguments do not meet these requirements, the error may occur. Additionally, if the input arguments are of different sizes or have mismatched dimensions, the error may also occur.

Can I use an alternative function to replace "diff" in my code?

Yes, there are alternative functions in MATLAB that can be used to compute differences between elements in a vector or matrix, such as "gradient" or "del2". It is important to consult the MATLAB documentation to determine which function best suits the specific needs of the code.

Is there a way to prevent the "Function 'diff' is Not Supported" error in MATLAB?

To prevent the error, it is important to carefully review the syntax and input arguments of the "diff" function before running the code. It may also be helpful to use conditional statements or try-catch blocks to handle potential errors and prevent the code from crashing. Additionally, staying updated with the latest version of MATLAB can help ensure that all functions are supported and avoid any compatibility issues.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
750
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top