about gradient and diff

The help of my matlab says: (in article "gradient -Gradient vector of scalar function")
-If x is a scalar, gradient(f,x) = diff(f, x).
This is wrong, right?
diff(f,2) should = diff( diff(f) ) % means 2nd order diff
while gradient (f, 2) means the grid increment is 2. which means, for a curve f-t, or f(t), t is a linear space and dt = t(2)-t(1) = 2
Let me know everybody~ Thanks~

5 Comments

Matt J
Matt J on 15 Jun 2013
Which gradient() and diff() methods are you referring to? The ones in the Symbolic Toolbox?
For base MATLAB, the help says this:
"Y = diff(X,n) applies diff recursively n times,resulting in the nth difference. Thus, diff(X,2) isthe same as diff(diff(X))."
Hi folks,
Thanks for discussion.
There are actually two different gradient functions - see Walter's answer.
How do you tell your code which one to use?
Yuji Zhang
Yuji Zhang on 15 Jun 2013
Edited: Yuji Zhang on 15 Jun 2013
I think, if your input y is a symbolic function, like y = x^2, then gradient(y) is symbolic gradient.
If the y is a numerical curve, like y = [1 2 3 4 ....]; then gradient(y) is the numerical gradient.

Sign in to comment.

 Accepted Answer

Walter Roberson
Walter Roberson on 15 Jun 2013

1 vote

Symbolic gradient, http://www.mathworks.com/help/symbolic/gradient.html, is the gradient vector of a scalar function, but numeric gradient, http://www.mathworks.com/help/matlab/ref/gradient.html is just "Numerical gradient". It is numerical gradient for which the "2" would mean a point spacing of 2, and it is the symbolic gradient for which the "2" would mean double differentiation.

1 Comment

Hi Walter~
Nice explanation! Thank you so much! I didn't know there were two different gradient functions. Yea, I see - makes sense. These two gradient functions are in different contexts.
Thanks again~

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!