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

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more