# How do I model a correction time that grows with a positioning error?

A robot that misses its target by some distance spends time putting it right. If each axis of the miss is normal, the size of the miss is Rayleigh. The square of that size is ChiSquare with 2 degrees of freedom. The script uses each as the correction time.

```python
from nagasim import build as nb


def busy_with(correction):
    """A robot re-positions after every pick; the correction time comes from the error."""
    m = nb.Model(time_limit=4000, warmup=0)
    m.add_resource(name="Robot")
    picks = m.create(distribution=nb.Exponential(rate=1 / 3))
    picks >> m.process(resource_name="Robot", distribution=correction) >> m.dispose()
    return m.run(replications=10, seed=1).kpi("resource/Robot/Usage % (snapshot)")


# The size of a two-axis placing error, each axis normal with sd 1, is Rayleigh(sigma=1): mean 1.25.
# Its square is ChiSquare with 2 degrees of freedom: mean 2. A correction that costs a minute per unit
# of error and one that costs a minute per squared unit are 42% and 67% busy.
print(f"cost grows with the error:          {busy_with(nb.Rayleigh(sigma=1)):.0f}% busy")
print(f"cost grows with the squared error:  {busy_with(nb.ChiSquare(df=2)):.0f}% busy")

```

Save it as `recipe-positioning-error.py` and run it with `python`. The script seeds itself, so the numbers below are the same on your machine.

```text
cost grows with the error:          41% busy
cost grows with the squared error:  66% busy

```

## How it works

1. `Rayleigh(sigma)` is the length of a two-axis error whose axes are normal with standard deviation `sigma`; its mean is 1.25 for `sigma = 1`.
2. `ChiSquare(df)` with `df = 2` is that length squared, with mean 2, for a cost that grows with the square of the miss.
3. One minute per unit of error loads the robot to about 42%, and one minute per squared unit to about 67%.

## Elements used

- Distributions: [Rayleigh](../../reference/distributions/rayleigh.md), [ChiSquare](../../reference/distributions/chi-square.md), [Exponential](../../reference/distributions/exponential.md).
- Blocks: [CREATE](../../reference/blocks/create.md), [PROCESS](../../reference/blocks/process.md), [DISPOSE](../../reference/blocks/dispose.md).
- Python: [`Model`](../../reference/api/Model.md).

Back to all [recipes](index.md).
