Published
Edited
Jul 12, 2018
Importers
Insert cell
Insert cell
Insert cell
class DefiniteIntegral
{
constructor()
{
if(this.constructor == DefiniteIntegral)
{
throw new Error("Need a concrete implementation");
}
}
delta(a, b, n)
{
return ( b - a ) / n;
}
compute(func, a, b)
{
if(a == b)
{
return 0.0;
} else if (a > b)
{
return -1 * this.compute(func, b, a);
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
class RiemannSum extends DefiniteIntegral
{
constructor(num_steps, handedness)
{
super();
this.num_steps = num_steps;
this.handedness = handedness;
}
compute(f, start, end)
{
var integral = 0.0;
let delta = this.delta(start, end, this.num_steps)
if(this.handedness == 1)
{
this.num_steps -=1;
}
for(var j = this.handedness; j < this.num_steps; j++) integral += f(j*delta);

return integral * delta;
}
}
Insert cell
Insert cell
Insert cell
// feel free to change this function
func = (x) => x
Insert cell
(new RiemannSum(num_steps, 1)).compute(func, 1,2)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more