In the harmonic Oscillator example can anyone answer why the output experiences a ‘dampening’ at the start (in the first 5 seconds) and then becomes stable?

# Oscillator Example

**arvoelke**#2

There is a lot to talk about here, as this is the interaction of a number of effects. But the really nice thing about Nengo is it lets us easily study these effects in isolation!

First of all, we can switch the neurons to “direct mode” by including `neuron_type=nengo.Direct()`

in the call to `nengo.Ensemble(..)`

. This bypasses both neural representation (tuning curves) and spiking from the simulated ensemble, which allows us to focus purely on the intended macroscopic dynamics of the system.

```
# Create the model object
model = nengo.Network(label='Oscillator')
with model:
# Create the ensemble for the oscillator
neurons = nengo.Ensemble(200, dimensions=2, neuron_type=nengo.Direct())
```

Curiously, the oscillation now expands outwards. If we were to run this further, we would see that it diverges off to infinity!

As it turns out, if we then reduce the time-step to `dt=1e-4`

(instead of the default of `dt=1e-3`

), this problem seems to go away (or be reduced considerably).

```
# Create the simulator
with nengo.Simulator(model, dt=1e-4) as sim:
...
```

We have recently published a few papers (Voelker & Eliasmith, 2017 and Voelker & Eliasmith, 2018) that talk about this problem and the solution. The short story is that Principle 3 from the NEF does not account for the discrete nature of digital simulations. It assumes that the time-step is “sufficiently small” (i.e., approaching continuous time). However, this problem can be overcome by discretizing the desired dynamics and applying an extension of Principle 3 using the Z-transform in the discrete-time domain. This provides a procedure for implementing dynamical systems that behave identically even with different time-steps. This avoids the need to make `dt`

smaller, which is costly from a simulation stand-point (it can be kept as is, or even be made larger).

For the example values of `dt=1e-3`

and `synapse=0.1`

, we get (approximately):

```
transform=[[0.994975, 1.00499158], [-1.00499158, 0.994975]]
```

instead of the example’s `transform=[[1, 1], [-1, 1]]`

. This matrix can be obtained numerically via the software package nengolib, and the analytical solutions are in the above papers (equations 21 and 4.7 for the first and second papers, respectively):

```
import nengolib
A, _, _, _ = nengolib.synapses.ss2sim(
nengolib.signal.LinearSystem(([[0, 10], [-10, 0]], [[1], [0]], [[1, 0]], [[0]])),
nengo.Lowpass(0.1), dt=0.001).ss
print(A)
```

(Note: The `10`

comes from `1/0.1`

for reasons related to the Principle 3 implementation in the example.)

And now we have a perfect oscillator! It is perfect in the sense that it is **equivalent** to numerically solving the differential equations, up to some machine epsilon precision.

However… there is still something more going on. Switching gears back to neurons (removing `Direct()`

mode), as you noticed, the example oscillator converged inwards to a steady limit cycle, instead of diverging outwards. The short explanation is that, since the decoders (the synaptic weights) are L2-regularized, the representation is biased towards under-approximating the output around the edge of the circle, thus pushing it inwards. I have a visualization of this at all points within the circle, as we modulate the direction of the oscillation:

These two competing forces — (1) inwards pressure from regularization, and (2) an outwards bias from the incorrect transform — happen to cancel out at the limit cycle that you observed.

We have seen already how to fix the second problem by changing the `transform`

. We can limit the effect of the first problem by reducing the amount of L2-regularization, and increasing the number of neurons to improve the neural representation.

Yet, there is a third effect introduced by spiking. I won’t go into this right now since it’s material I need to unravel a bit more for my thesis, but you can see this by switching between `neuron_type=nengo.LIF()`

and `neuron_type=nengo.LIFRate()`

. An open challenge is to minimize the change in dynamics introduced by this latter switch!

As always, let me know if you have any follow-up questions and I can go into more detail.