Matlab gradient does not follow exact numerical formula
Afficher commentaires plus anciens
When the numerical gradient is computed, Matlab uses forward differences for the end points, and uses central differences for the interior points. However, when looking at the code, the central differences equation that they use does not divide by 2h. Here is their code:
% Take forward differences on left and right edges
if n > 1
g(:,1) = (f(:,2) - f(:,1))/(h(2)-h(1));
g(:,n) = (f(:,n) - f(:,n-1))/(h(end)-h(end-1));
end
% Take centered differences on interior points
if n > 2
h = h(3:n) - h(1:n-2);
g(:,2:n-1) = (f(:,3:n) - f(:,1:n-2)) ./ h;
end
The equation for central differences has a 2h in the denominator, i.e.,
f'(x) = (1/2h) * (f(x+h) - f(x-h)).
Why is Matlab not using this scaling factor?
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Function Creation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!