# System dynamics

Delays, smoothing, test inputs and finance for stock-and-flow models; most need `simulation.sd`.

Each value is the expression read as an auxiliary at t = 20 of a small model (time step 0.5) with a resource `Desk` of capacity 2 and a stream of arrivals that outruns it, a level `Tank` filled by the flow `fill`, a conversion `Digester` into the level `Holder`, a delay, a series, a table, a variable, a power draw, a two-node network and a subscript `region` with three elements.

## Typical uses

Use these functions inside a flow rate, an auxiliary or a delay input of a stock-and-flow model. The `SMOOTH` family models how a decision maker perceives a changing value. The `DELAY` family models material in transit, such as an order that takes time to arrive. The time functions `STEP`, `PULSE` and `RAMP` are test inputs. The rest read, extrapolate or discount a signal.

## Behaviour and rules

`tau` is a time in the model's time unit. `SMOOTH` and `SMOOTH3` are information delays of order 1 and 3. They average the input and do not conserve a quantity. `DELAY1`, `DELAY3` and `DELAYN` are material delays of order 1, 3 and n, where n must be a literal integer of 1 or more. They conserve the quantity: what enters comes out. A higher order gives a sharper step response.

`DELAYP` returns the input exactly `tau` later, from a stored history. The other delays integrate stages and therefore need `simulation.sd`. `DELAYP` does not. Each call in an expression gets its own hidden stages. A call is allowed only in a flow rate, an auxiliary or a delay input, and `tau` must be above 0.

The optional `init` is the value of the output at the start. For the `SMOOTH` and `DELAY` family it defaults to the input's value at time 0, so the model starts in balance. For `DELAYP` it defaults to 0.

`STEP(h, t0)` is 0 before `t0` and `h` from then on. `PULSE(h, t0, w)` is `h` from `t0` for a width of `w`. `RAMP(s, t0)` rises with slope `s` after `t0`, and a third argument holds it from there. `INITIAL(x)` freezes the value `x` had at time 0. `ACTIVE_INITIAL(x, init)` gives `init` while initial values are set up and `x` afterwards.

`TREND` is a fractional rate of change, `FORECAST` extends it ahead by a horizon, and `NPV` discounts a stream at a rate. `SAMPLE_IF_TRUE` holds the last `x` read while its condition was true. `NOISE_NORMAL` is a normal draw clipped to a range, fixed for each call site and seed.

## ACTIVE_INITIAL

`ACTIVE_INITIAL(x, init)`: 2 arguments.

x while running, init when initial values are set up.

Example: `ACTIVE_INITIAL(STEP(10, 5), 1)` gives 10.

## DELAY1

`DELAY1(x, tau[, init])`: 2 to 3 arguments.

first-order material delay (init defaults to x at t0); needs simulation.sd.

Example: `DELAY1(STEP(10, 5), 4)` gives 9.765.

Used in the bundled models Workforce (goal-seeking).

## DELAY3

`DELAY3(x, tau[, init])`: 2 to 3 arguments.

third-order material delay (init defaults to x at t0); needs simulation.sd.

Example: `DELAY3(STEP(10, 5), 4)` gives 9.99.

Used in the bundled models Supply line: order-rate management game, Inventory with lead time.

## DELAYN

`DELAYN(x, tau, n[, init])`: 3 to 4 arguments.

n-th order material delay, n a literal integer (init defaults to x at t0).

Example: `DELAYN(STEP(10, 5), 4, 2)` gives 9.953.

## DELAYP

`DELAYP(x, tau[, init])`: 2 to 3 arguments.

x exactly tau later (pipeline); no grid needed.

Example: `DELAYP(STEP(10, 5), 4)` gives 10.

## FORECAST

`FORECAST(x, avg_time, horizon)`: 3 arguments.

x extrapolated horizon ahead along its TREND.

Example: `FORECAST(RAMP(1, 0) + 10, 4, 5)` gives 35.72.

## INITIAL

`INITIAL(x)`: 1 argument.

the value x had at t=0, held for the whole run.

Example: `INITIAL(TNOW + 7)` gives 7.

## NOISE_NORMAL

`NOISE_NORMAL(mean, sd, lo, hi)`: 4 arguments.

a normal draw clipped to [lo, hi], redrawn at each re-evaluation, fixed per call site and seed.

Example: `NOISE_NORMAL(0, 1, -2, 2)` gives 1.237.

## NPV

`NPV(stream, rate, init, factor)`: 4 arguments.

present value of a stream discounted at rate, from init, times factor; needs simulation.sd for the step.

Example: `NPV(STEP(100, 0), 0.05, 0, 1)` gives 1,296.

## PULSE

`PULSE(h, t0, w)`: 3 arguments.

h on [t0, t0 + w), else 0.

Example: `PULSE(5, 2, 3)` gives 0.

## RAMP

`RAMP(s, t0[, t1])`: 2 to 3 arguments.

s * (TNOW - t0) after t0, held after t1.

Example: `RAMP(2, 4)` gives 32.

## SAMPLE_IF_TRUE

`SAMPLE_IF_TRUE(cond, x, init)`: 3 arguments.

the last x read while cond was true, init before the first true reading.

Example: `SAMPLE_IF_TRUE(TNOW < 10, TNOW, 0)` gives 9.5.

## SMOOTH

`SMOOTH(x, tau[, init])`: 2 to 3 arguments.

first-order information delay (init defaults to x at t0); needs simulation.sd.

Example: `SMOOTH(STEP(10, 5), 4)` gives 9.769.

## SMOOTH3

`SMOOTH3(x, tau[, init])`: 2 to 3 arguments.

third-order information delay (init defaults to x at t0); needs simulation.sd.

Example: `SMOOTH3(STEP(10, 5), 4)` gives 9.991.

## STEP

`STEP(h, t0)`: 2 arguments.

h from t0 on, else 0.

Example: `STEP(10, 5)` gives 10.

Used in the bundled models Supply line: order-rate management game.

## TREND

`TREND(x, avg_time[, init])`: 2 to 3 arguments.

fractional rate of change of x against its avg_time-smoothed value (init: the starting trend, default 0).

Example: `TREND(RAMP(1, 0) + 10, 4)` gives 0.03816.

## See also

- [All function groups and the grammar](index.md), [Distributions](../distributions/index.md)
- Guides that use them: [System dynamics](../../system-dynamics.md), [How do I test a model with a step, a ramp and a pulse?](../../how-to/recipes/test-inputs.md), [How do I delay goods in transit by a fixed time or a spread of times?](../../how-to/recipes/transit-delay.md), [How do I smooth a noisy signal?](../../how-to/recipes/smooth-noise.md), [How do I extrapolate a growing trend?](../../how-to/recipes/trend-forecast.md), [How do I discount a stream of cash?](../../how-to/recipes/discounted-cash.md) and 1 more
