How to increment global variable from function in python -


i'm stuck simple increment function like

from numpy import * pylab import *  ## setup parameters , state variables t       = 1000                # total time simulate (msec) dt      = 1                   # simulation time step (msec) time    = arange(0, t+dt, dt) # time array vr      = -70                 #reset el      = -70                  ## lif properties vm      = zeros(len(time))      # potential (v) trace on time  rm      = 10                    # resistance (mohm) tau_m   = 10                    # time constant (msec) vth     = -40                   # spike threshold (v)  ## input stimulus       = 3.1                 # input current (na) vm[0] = -70  fr = 0  ## iterate on each time step def func(ie, vm, fr):     i, t in enumerate(time):         if == 0:             vm[i] = -70         else:              vm[i] = vm[i-1] + (el- vm[i-1] + ie*rm) / tau_m * dt             if vm[i] >= vth:                 fr += 1                 vm[i] = el      return  ie = 3.1 func( ie, vm, fr) print fr  ## plot membrane potential trace   plot(time, vm) title('leaky integrate-and-fire') ylabel('membrane potential (mv)') xlabel('time (msec)') ylim([-70,20]) show() 

why after func called, fr still 0?

i know it's simple have wasted long time on this

thank

you have 2 fr variables in different scopes

fr = 0 

is outside of function, never changed.

fr += 1 

is inside function , incremented, different variable.

here solution (one of possible ones):

def func(ie, vm, fr):     i, t in enumerate(time):         if == 0:             vm[i] = -70         else:              vm[i] = vm[i-1] + (el- vm[i-1] + ie*rm) / tau_m * dt             if vm[i] >= vth:                 fr += 1                 vm[i] = el      return fr 

then, do

fr = func(ie, vm, fr) 

one more tip. if fr variable 0 default can this:

def func(ie, vm, fr=0): 

when defining function, , pass third paramenter when need different 0.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -