.. currentmodule:: brian2

.. state_variables:

Example: state_variables
========================


        .. only:: html

            .. |launchbinder| image:: file:///usr/share/doc/python-brian-doc/docs/badge.svg
            .. _launchbinder: https://mybinder.org/v2/gh/brian-team/brian2-binder/master?filepath=examples/synapses/state_variables.ipynb

            .. note::
               You can launch an interactive, editable version of this
               example without installing any local files
               using the Binder service (although note that at some times this
               may be slow or fail to open): |launchbinder|_

        

Set state variable values with a string (using code generation).

::

    
    from brian2 import *
    
    G = NeuronGroup(100, 'v:volt', threshold='v>-50*mV')
    G.v = '(sin(2*pi*i/N) - 70 + 0.25*randn()) * mV'
    S = Synapses(G, G, 'w : volt', on_pre='v += w')
    S.connect()
    
    space_constant = 200.0
    S.w['i > j'] = 'exp(-(i - j)**2/space_constant) * mV'
    
    # Generate a matrix for display
    w_matrix = np.zeros((len(G), len(G)))
    w_matrix[S.i[:], S.j[:]] = S.w[:]
    
    subplot(1, 2, 1)
    plot(G.v[:] / mV)
    xlabel('Neuron index')
    ylabel('v')
    subplot(1, 2, 2)
    imshow(w_matrix)
    xlabel('i')
    ylabel('j')
    title('Synaptic weight')
    tight_layout()
    show()
    

