% [dx, ddx] = diffCenterVar(t,x);
% Computes first and second derivatives of x(t)
% --> Second-order accurate
% --> Works for non-uniform grid
% t = [1,m] = time grid
% x = [n,m] = function values for each point on the grid
% dx = [n,m] = first derivative of x
% ddx = [n,m] = second derivative of x
% This function works by locally fitting a quadratic curve between each
% set of three points. See Derive_Eqns.m for details.
Matthew Kelly (2021). diffCenterVar (https://www.mathworks.com/matlabcentral/fileexchange/58287-diffcentervar), MATLAB Central File Exchange. Retrieved .
Simple fix for endpoints compared to Matlab's gradient.m.
For a drop-in replacement, redefine the gradient function:
gradient = @(x,t) reshape(diffCenterVar(t(:).', x(:).'), size(x));
Inspired by: diffCenter
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!