How does Symbolic diff() Operate When the Order of Derivative is Symbolic?

2 views (last 30 days)
Simple symbolic function
syms n integer positive
syms x real
f(x) = x^n
f(x) = 
First, second, and sixth (just to check) derivatives as expected (or at least justified assuming n >= order of derivative)
[diff(f(x),x,1) diff(f(x),x,2) diff(f(x),x,6)]
ans = 
Taking the nth derivative returns a peculiar result.
diff(f(x),x,n)
ans = 
Actually, I was quite surprised that call didn't generate an error as I thought the third argument had to be an actual number, not a symbolic variable. In any case, it returned w/o error, but the result looks incorrect.
Try a different function that is not defined in terms of n
g(x) = x^5;
diff(g(x),x,n)
ans = 
0
Seems like this result, if anything, should have returned as a piecewise for the cases when n <= 5 and otherwise. Returning 0 doesn't seem like the right thing to do.
As an aside, trying the n+1th derivative of f(x) results in an error. The error message indicates the differentiation order can be a symbolic variable, though that's not clear from the symbolic diff doc page.
diff(f(x),x,n+1)
Error using sym/diff (line 79)
Second and third arguments must either be both variables, or a variable and a nonnegative integer specifying the number of differentiations.

Accepted Answer

Shishir Reddy
Shishir Reddy on 11 Jul 2024
Hi Paul,
As per my understanding, you would like to get more clarification on how “diff” function works in MATLAB when operated using symbolic variables.
Let me discuss few of the scenarios for f(x) = x^n when passed as input to the “diff” function.
  • Case 1 : diff(f(x),x,1) - Output of this will be the derivative of function “f” with respect to “x”, which is n*x^(n-1)
  • Case 2 : diff(f(x),x,2)- This gives the second derivative of “f” with respect to “x” which is n*(n-1)*x^(n-2)
  • Case 3 : diff(f(x), x, n) - This means that “f” is differentiated with respect to “x” and the result is then differentiated with respect to “n”. Which means the output will be x^(n-1) + n*log(x)*x^(n-1)
  • Case 4 : diff(x^5, x, n) - As stated in Case 3, x^5 is first differentiated with respect to “x”, which gives 5*x^4. Now, if this is differentiated with respect to “n”, we get 0 because 5*x^4 is independent of n.
  • Case 5 : diff(f(x), x, n+1) -This gives error because performing arithmetic operations is not allowed between symbolic variables and constants.
For further reference, please refer to the following documentation:
I hope this helps.

More Answers (0)

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!