Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function.prototype.diff = function(eps) {
- var fn = this, eps = eps || 1e-6;
- return function(x) {
- return (fn(x+eps) - fn(x)) / eps;
- };
- };
- (function(x) { return x*x }).diff()(6)
- // -> 12
- Function.prototype.int = function(n) {
- var fn = this, n = n || 10000;
- return function(a,b) {
- var s = 0, i, eps = (b-a)/n;
- for (i = 0; i < n; i ++)
- s += fn(a + i*eps) * eps;
- return s;
- };
- };
- (function(x) { return x*x }).int()(0,4)
- // -> 21.33
Add Comment
Please, Sign In to add comment