Simple symbolic function
f(x) = x^n
f(x) = ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1728981/image.png)
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 = ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1728986/image.png)
Taking the nth derivative returns a peculiar result.
diff(f(x),x,n)
ans = ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1728991/image.png)
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
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.