ƒ

calculang ✍️ editable and dangerous! 🧙‍♂️⚠️
javascript ✨ generated from calculang ⬆️
dev tools 🧰

reactive workings (todo):

🎨 Pi by lattice 🥮

inputs ⚙️

Press play for an improving approximation of π based on approximating the area of a unit circle using a lattice!

calculated area inside unit circle = units2 (1 quadrant); *4 ⇒

π ≈

⇒ error ≈

(using πr2 and r=1)

inspiration 🧙

A Geogebra example I found linked in the wikipedia article for Pi. I replicate this approach/numbers.

Given this is similar but simpler than the Monte Carlo Pi approximation I reproduced last year, I'm not sure why this approach isn't more common.

const spec = ({
  // vega-lite
  title: "points",
  mark: {type:'point', tooltip:true, filled:true},
  encoding: {
    x: { field: 'x', type: 'quantitative', scale: { domain: [0,1]} },
    y: { field: 'y', type: 'quantitative', scale: { domain: [0,1]}  },
    color: {field: 'inside', type: 'nominal', sort: 'descending'},
    detail: {field: 'i_in', type: 'nominal'},
  },
  data: { name: "data" },
  autosize: { "type": "fit", "contains": "padding"},
  // no reactive w/h due to https://github.com/observablehq/framework/issues/1194
  width: 400,//Math.min(500,content_width-30),//Math.min(400,content_width),
  height: 400-30,//Math.min(500,content_width-30)/1.2,//Math.min(400,content_width-30),
  background:'rgba(0,0,0,0)'
})

// interactivity via vega signals and listeners
const viz = embed('#viz', spec)
const data_source = calcuvegadata({
  models: [model],
  spec,
  domains: {
    i_in: _.range(0,n_in*n_in)
  },
  input_cursors: [
    { n_in }
  ]
})
viz.view.data("data", data_source)/*.resize()*/.run(); // turn off resize
📜
const pis = [5, 10, 20,30,50,55,60,65,70,75]
  .map(n_in => ({n_in, pi_approximation: model.pi_approximation({ n_in}), proportion_inside: model.proportion_inside({ n_in}), error: model.error({ n_in})}))

display(Inputs.table(pis, {sort: 'n_in', reverse: true, format: { pi_approximation: d3.format(',.10f'), error: d3.format(',.4f') }}))

⚠️ This π approximation is not suitable for space travel.

For better approximations check my post from last year. See also, separate approximation using Monte Carlo methods.