Functional Implementation of Bathtub Models

Overview


In the functional implementation of a bathtub model, the state of the model is represented by an object, and the evolution of the model over time is given by a specified function.

Basic Setup


The state of the model at a point in time is given by an object.


let state= {
	orders:20,
	rawMaterials:0,
	finishedProduct:0,
	shippedProduct:0
};
					

Single Step Evolution


A function is created which takes the current state of the model and returns the next state.


function next(state){
	let result = {};
	result.orders = 20;
	result.rawMaterials = state.orders + state.rawMaterials * 0.1;
	result.finishedProduct = state.rawMaterials *0.9;
	result.shippedProduct = state.finishedProduct + state.shippedProduct;
	return result;
}
					

Evolution Over Time


The evolution of the state can be run over time. In this example, the code calculates the evolution over 20 steps.


let series = [state];
$range(1,20).forEach(p=>{
	state = next(state);
	series.push(state);
})
					
Try it!