Clear Filters
Clear Filters

diff vs. gradient

57 views (last 30 days)
Benjamin
Benjamin on 5 Nov 2018
Edited: Matt J on 5 Nov 2018
Why does:
diffpressure = gradient(pressure)./gradient(density);
and
diffpressure = diff(pressure)./diff(density);
give drastically different results? The derivative using each method are completely different. See attached figure

Accepted Answer

Matt J
Matt J on 5 Nov 2018
Edited: Matt J on 5 Nov 2018
gradient uses central differences while diff(x) uses right-hand differences x(i+1)-x(i).
  12 Comments
Star Strider
Star Strider on 5 Nov 2018
Yes!
Matt J
Matt J on 5 Nov 2018
Edited: Matt J on 5 Nov 2018
If the X-data are not equi-spaced, use diff().

Sign in to comment.

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!