Public
Edited
Oct 12, 2023
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function cobweb_pic(r, x0, opts_in = {}) {
// let f = x => r * x * (1 - x);
// let f = (x) => r * Math.sin(x);
let f = (x) => lambda2 * Math.sin(x);
let { w = 800, h = 400 } = opts_in;
let opts = {
xdomain: [-2 * Math.PI, 2 * Math.PI],
ydomain: [-Math.PI, Math.PI],
marginTop: (10 * w) / 300,
marginRight: (10 * w) / 300,
marginBottom: 0, //(10 * w) / 300,
marginLeft: 0, //(10 * w) / 300,
inset: 6,
insetTop: 6,
insetRight: 6,
insetBottom: 0,
insetLeft: 0,
width: w,
height: h,
strokeWidth: 2.5,
xticks: [0],
yticks: false
};
let x_scale = d3.scaleLinear(opts.xdomain, [
opts.marginLeft + opts.insetLeft,
opts.width - opts.marginRight - opts.insetRight
]);
let y_scale = d3
.scaleLinear(opts.ydomain, [
opts.height - opts.marginBottom - opts.insetBottom,
opts.marginTop + opts.insetTop
])
.nice();
let pts_to_path = d3
.line()
.x(function (d) {
return x_scale(d[0]);
})
.y(function (d) {
return y_scale(d[1]);
});

let plot = funplot([f, (x) => x], opts);

// let x0 = Math.PI / 2;
let cobweb = make_cobweb(f, x0, -2 * Math.PI, 2 * Math.PI);
let svg = d3.select(plot);
svg
.append("path")
.attr("d", pts_to_path(cobweb))
.style("stroke", "black")
.style("stroke-width", 0.4)
.style("fill", "none")
.style("opacity", 0.4);
svg
.append("line")
.attr("x1", 0)
.attr("x2", w)
.attr("y1", y_scale(0))
.attr("y2", y_scale(0))
.attr("stroke", "black");
svg
.append("line")
.attr("y1", 0)
.attr("y2", h)
.attr("x1", x_scale(0))
.attr("x2", x_scale(0))
.attr("stroke", "black");
svg
.append("circle")
.attr("cx", x_scale(x0))
.attr("cy", y_scale(x0))
.attr("fill", "#3d3")
.attr("stroke", "black")
.attr("r", 3);

return plot;
}
Insert cell
function make_cobweb(f, x0, xmin, xmax) {
let x = x0;
let y = x;
let bail = 1500;
let cobweb = [[x, y]];
let i = 0;
while (i < bail && xmin <= y && y <= xmax) {
y = f(x);
cobweb.push([x, y]);
x = y;
cobweb.push([x, y]);
i++;
}
return cobweb;
}
Insert cell
function F(x, n) {
let xx = x;
for (let i = 0; i < n; i++) {
xx = f(xx);
}
return xx;
}
Insert cell
f = (x) => lambda * Math.sin(x)
Insert cell
import { funplot } from "@mbostock/funplot"
Insert cell
import { build_samples } from "@mcmcclur/adaptive-plotter"
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