Scientific Calculator

(public) moshangcheng/calcIntegral

By moshangcheng moshangcheng

calculate integral

Tagged: integral

/***example
calcIntegral(function(x) {
        e^(-pi*x^2);
}, 0, function(x, value) {
        abs(value) > 0.0001;
}, function(x, value) {
        abs(value) > 0.0001;
});
***/
var calcIntegral = function(v, center, leftCnd, rightCnd) {
	var sum = 0.0;
	var delta = 0.001;
	var x = center;
	var value = v(x);
	while(rightCnd(x, value)) -> (
		sum += value * delta;
		x += delta;
		value = v(x);
	);
	x = center;
	value = v(x);
	while(leftCnd(x, value)) -> (
		sum += value * delta;
		x -= delta;
		value = v(x);
	);
	sum;
};

spam? | offensive?

1 Comment

  • //example
    calcIntegral(function(x) {
    e^(-pi*x^2);
    }, function(x, value) {
    abs(value) > 0.0001;
    }, function(x, value) {
    abs(value) > 0.0001;
    });

Sign in to leave a comment