Skip to content

Instantly share code, notes, and snippets.

@deanh
Created September 17, 2010 21:21
Show Gist options
  • Save deanh/584979 to your computer and use it in GitHub Desktop.
Save deanh/584979 to your computer and use it in GitHub Desktop.
function d(f, h) {
h = h || 0.001;
return function (x) {
return (f(x+h)-f(x))/h;
}
}
function S(f, h) {
h = h || 0.001;
return function(x0, a) {
var sum = 0;
for (x=x0; x<=a; x=x+h) {
sum = sum + (f(x)*h);
}
return sum;
}
}
function f1(x) {return 1;} // undefined
df = d(f1);
// function (x) {
// return (f(x+h)-f(x))/h;
// }
df(1); // 0
df(10); // 0
I = S(f1);
// function (x0, a) {
// var sum = 0;
// for (x=x0; x<=a; x=x+h) {
// sum = sum + (f(x)*h);
// }
// return sum;
// }
I(1,2); // 1.0010000000000006
function f2(x) {return 5*x;} // undefined
df2 = d(f2)
// function (x) {
// return (f(x+h)-f(x))/h;
// }
df2(5); // 5.000000000002558
df2(10000000); // 5.000002682209015
I2 = S(f2);
// function (x0, a) {
// var sum = 0;
// for (x=x0; x<=a; x=x+h) {
// sum = sum + (f(x)*h);
// }
// return sum;
// }
I2(0,1); // 2.497500000000002
I2(0,2); // 10.004999999999725
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment