diff vs. gradient

156 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
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)

Community Treasure Hunt

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

Start Hunting!