Published
Edited
Jul 10, 2020
Fork of RWKPlotLib
1 fork
Importers
1 star
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
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof a_integrate = def`async generator function integrate(f, min, max, steps=2*width):
Integrate the function _f_ from _min_ to _max_.
For the moment, we'll use the trapezoid approximation.

This is the same as \`integrate()\`, except as an asynchronous generator so we can animate
the integration. Return values are of the form _[x, y, integral-so-far]_.

May also take a _range_ instead of _min_, _max_, _steps_.

* _steps_: The default is chosen for oversampling the screen resolution.
`(function* a_integrate(f, min, max, steps = 2 * width) {
if (typeof min === 'number') {
yield* a_integrate(f, range(min, max, steps));
} else if (min) {
const r = min;
function* pgen() {
for (const x of r) {
yield [x, f(x)];
}
}
yield* a_integrate(pgen());
} else {
const r = f;
let total = 0;
let v = r.next();
if (v.done) {
yield [0, 0, 0];
return;
}
let [xprevious, yprevious] = v.value;
for (const [x, y] of r) {
const mid = (y + yprevious) / 2;
const delta = x - xprevious;
total += mid * delta;
yprevious = y;
xprevious = x;
yield [x, y, total];
}
}
})
Insert cell
a_integrate(() => 1, range(1, 2, 0.01))
Insert cell
Insert cell
Insert cell
Insert cell
import { range } from '@bobkerns/utilities'
Insert cell
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