Skip to content

Instantly share code, notes, and snippets.

@guipn
Created June 17, 2011 20:43
Show Gist options
  • Save guipn/1032298 to your computer and use it in GitHub Desktop.
Save guipn/1032298 to your computer and use it in GitHub Desktop.
Numeric Derivative
function derivator(func, step) {
step = step || Number.MIN_VALUE;
return function (x) {
return (func(x + step) - func(x)) / step;
};
}
// As simple as that.
//
// var derive = derivator(function(x) { return x * x; }, 1e-10);
//
// derive(1) now (approximately) equals the derivative of x^2 for x = 1. The smaller the step,
// the closer the approximation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment