Notebooks 2.0 is here.

Published
Edited
Feb 12, 2021
1 star
Insert cell
md`
# TokenCAD Examples
`
Insert cell
md`
## Example: Population & food supply


This example was inspired by the cadCAD online course recently published, available at https://cadcad.education

${tex.block`\begin{aligned}
food_{t} = food_{t-1} - \alpha * population_{t-1}\\

population_{t} = population_{t-1} + \beta * food_{t-1}
\end{aligned}`}


This example is simply a single-run simulation and displays a time series. It models a simple two-dimensional system: a population and its food supply.

You can also comment out the time series plot below and uncomment the phase space plot!

I invite you to improve the sophistication and realism of this model; there are many hidden assumptions that can be added to make it more interesting and insightful!

`
Insert cell

{

let initialState = {
population: 50,
food: 1000
}


let sysParams = {
reproduction_rate: 0.02, // Beta
consumption_rate: 0.01 // Alpha
}

let updateFuncs = {
population: 'reproduction_rate * food',
food: '- consumption_rate * population'
}
let updateBlocks = [
['food', 'population']
]
let experiment = new Experiment(1000, initialState, updateFuncs, sysParams, updateBlocks)
let results = experiment.run()

return [ experiment.plotTimeSeries("time"),
experiment.plotPhaseSpace("phase")]
}

Insert cell
Insert cell
md `
# Predator-Prey

`
Insert cell
{
// If you want to run a parameter sweep
let stateTemplate = {
variableNames: ['x', 'y'],
stateVariables: [
{
name: 'x',
min: -2,
max: 2.5,
interval: 0.5
},
{
name: 'y',
min: -2,
max: 2.5,
interval: 0.5
}
]
}
let initialStateSequence = generateInitialStatesSequence(stateTemplate)

// Doing a single run
let initialState = {x: 1, y: 1}
let sysParams = {
r: 1,
b: 1,
d: 1,
c: 1,
K: 10
}
let updateFuncs = {
x: 'r * x * (1 - x / K) - (1 - 1 / (b * y + 1)) * x',
y: '-d * y + c * x * y'
}
// Updates in parallel
let updateBlocks = [
['x', 'y']
]
// Run for 100 timesteps
let experiment = new Experiment(100, initialState, updateFuncs, sysParams, updateBlocks)
// Only useful if you passed initialStateSequence as second arg above, indicating a param sweep
// Otherwise, unnecessary
let results = experiment.run()
// return results

//return experiment
//return experiment.getAllStates()
return [experiment.plotTimeSeries("time2"),
experiment.plotPhaseSpace("phase2")]

}
Insert cell
html `<div id="time2"></div><div id="phase2"></div>`

Insert cell
import {Experiment, generateInitialStatesSequence} from "@tannr/tokencad";
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