File Exchange

image thumbnail

Finite difference weights

version (1.77 KB) by Toby Driscoll
Finds finite difference (or interpolation) weights for an arbitrary set of nodes in one dimension.

1 Download

Updated 06 Feb 2007

View License

Given a vector of nodes x, a point of interest xi, and a nonnegative order of derivative m, this function returns weights such that an inner product with the values f(x) returns an approximation to f^(m)(xi). Based on the recursive formula due to Fornberg (A Practical Guide to Pseudospectral Methods, Cambridge University Press).

Examples from the file:

% First derivative near a boundary
h = 0.4; x = h*(0:5)';
w = fdweights(x(2),x,1)
[ w'*exp(x) exp(x(2)) ]

% Interpolation
x = [0 0.25 1 1.5 2.5]';
w = fdweights(0.5,x,0)
[ w'*sin(x) sin(0.5) ]

Cite As

Toby Driscoll (2021). Finite difference weights (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)

Martin Storath

Toby Driscoll

"Order" can be a slippery concept when the points are not equally spaced and so refined. That said, if their spacing is scaled to zero uniformly, the order of accuracy will be at least (# of nodes) - (order of derivative) for sufficiently smooth functions. This can increase with symmetry (e.g., centered approximations to the second derivative).

John D'Errico

Not bad, but it could be better. I like that it has an H1 line, it has examples of use, good help, useful internal comments, plus a reference.

What did I want to see? I always look for error checks, to verify the arguments are meaningful - there were no error checks. But most importantly, I wanted to see some indication of the order of these approximations. Will these be first order approximations or second order? (This is distinct from derivative order of course.) This documentation was totally lacking. Even better would be to provide the user with a parameter that would allow them to control that order of approximation.

MATLAB Release Compatibility
Created with R2006b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: Easy build finite-difference operators

Community Treasure Hunt

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

Start Hunting!