Why gradient and diff give different results
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ki
am 8 Feb. 2016
Beantwortet: the cyclist
am 8 Feb. 2016
Hi there, I am trying to derive a force from given potential, that F = - gradient of potential if mass is one. To test it, I try to set up a very simple 2D case with potential is
A=[1 2 3; 3 2 6; -2 -7 2];
dx=1;
dy=1;
[fx, fy]=gradient(A, dx, dy);
Now I am thinking to use diff instead. ax=diff(A')/dx; ay=diff(A)/dy;
Ignore the fact that diff will lose 1 row or column, the numbers of results for those two methods are not in agreement as well. So why is that? Besides differentiation, what else gradient calculate?
0 Kommentare
Akzeptierte Antwort
the cyclist
am 8 Feb. 2016
The exact algorithm that gradient uses is given at the bottom of the documentation page for gradient.
It uses the central difference (two-sided difference, divided by 2) for central points, and single-side difference at the edges.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!