human
Write me a Python code to compute a nonlinear function of an input ($d$) and the state variable ($x$) given the following:
I am working on the file "SYDE 556 Lecture 5 Dynamics.ipynb".
The first task is:
That also looks right.  Note that as $\tau_c \rightarrow \infty$ this will approach the integrator.
Humans (a) and Goldfish (b)
Humans have more neurons doing this than goldfish (~1000 vs ~40)
They also have slower decay (70 s vs. 10 s).
Why do these fit together?

<img src="files/lecture5/integrator_decay.png">
Controlled Integrator

What if we want an integrator where we can adjust the decay on-the-fly?
Separate input telling us what the decay constant $d$ should be

$\dot{x} = -d x + v$

So there are two inputs: $v$ and $d$

This is no longer in the standard $Ax + Bu$ form. Sort of...

Let $A = -d(t)$, so it's not a matrix
But it is of the more general form: ${dx \over dt}=f(x)+g(u)$


We need to compute a nonlinear function of an input ($d$) and the state variable ($x$)

How can we do this?
Going to 2D so we can compute the nonlinear function
Let's have the state variable be $[x, d]$


<img src="files/lecture5/controlled_integrator.png" width = 400>
Can you write Python code for it?
assistant
#Here is Python code to compute a nonlinear function of an input ($d$) and the state variable ($x$)
.

import nengo
from nengo.utils.functions import piecewise

tau = 0.1

model = nengo.Network('Controlled integrator', seed=1)

with model:
    vel = nengo.Node(piecewise({.2:1.5, .5:0 }))
    dec = nengo.Node(piecewise({.7:.2, .9:0 }))
    
    velocity = nengo.Ensemble(100, dimensions=1)
    decay = nengo.Ensemble(100, dimensions=1)
    position = nengo.Ensemble(400, dimensions=2)
    
    def feedback(x):
        return -x[1]*x[0]+x[0], 0
    
    conn = nengo.Connection(vel, velocity)
    conn = nengo.Connection(dec, decay)
    conn = nengo.Connection(velocity, position[0], transform=tau, synapse=tau)
    conn = nengo.Connection(decay, position[1], synapse=0.01)
    conn = nengo.Connection(position, position, function=feedback, synapse=tau)

    position_p = nengo.Probe(position, synapse=.01)
    velocity_p = nengo.Probe(velocity, synapse=.01)
    decay_p = nengo.Probe(decay, synapse=.01)
    
sim = nengo.Simulator(model)
sim.run(1)

plot(sim.trange(), sim.data[decay_p])
lineObjects = plot(sim.trange(), sim.data[position_p])
plot(sim.trange(), sim.data[velocity_p])
legend(('decay','position','decay','velocity'),loc="best");

from nengo_gui.ipython import IPythonViz
IPythonViz(model, "controlled_integrator.py.cfg")