How to add value to variable in loop in function
I had a very long program that I tried to shorten by adding a function.
The principle of the following code is that I add a value to a certain
array each time the code goes through the loop but with the function I
define the variable to add the value to with the variable from the
function and it give me always the same value in the array whatever I try
to put in the function.
Here's the code:
def pointmilieu(Etof,canaux_E,Etof_mid,canaux_mid,sigma):
Bin_ini = Bin_initial = canal_debut
Bin_i = interval
''' Bin_ini est le canal de depart pour la boucle la premiere fois
qu'elle roule
Bin_i est le bon initial
Bin_initial est le canal de depart que la boucle utilisera a
chaque fois(il changera)'''
for i in arange(0,1000,1):
'''ici on donne un range au hasard par bon de 1 pour que i roule
autant de fois que necessaire'''
Bin_final = Bin_ini + Bin_i*(i+1)
'''Bin_final prend le Bin_inital et lui rajoute Bin_i(bon) * i+1
pour qu'il soit toujours plus grand'''
figure(99)
####################
range_E=Etof[(canaux_E >= Bin_initial) & (canaux_E <= Bin_final)]
#####################
Bins=sqrt(len(range_E))
troppetit = Bins<5
while troppetit:
troppetit= False
Bin_i += 1
Bin_final = Bin_ini + Bin_i*(i+1)
if Bin_final > np.max(canaux_E):
break
range_E=Etof[(canaux_E>=Bin_initial) & (canaux_E <= Bin_final)]
Bins=sqrt(len(range_E))
if Bins<5:
troppetit=True
else :
break
if Bin_final > np.max(canaux_E):
break
hE=hist(range_E,bins=Bins)
ydata=hE[0]
xdata=hE[1]
dxdata=(xdata[1]-xdata[0])
xdata+=dxdata/2
xdata=np.insert(xdata,0,xdata[0]-dxdata)
ydata=np.insert(ydata, 0,0)
ydata=np.append(ydata, 0)
x0=np.array([np.max(ydata),xdata[(np.where(ydata==np.max(ydata)))][0],1])
g=curve_fit(Gauss,xdata,ydata,x0)
g0=g[0]
g1=g[1]
############################
############################
Etof_mid[i]=g0[1]
canaux_mid[i]=(Bin_initial+Bin_final)/2
sigma[i] = sqrt(g1[1,1])
'''it is here where it gets funky, at each iteration i add value
to the variable of the function but in the end it do not add what
i want and whatever i put in the function the Etof_mid and
canaux_mid will always give me the same value. When it was only a
loop without the function part it worked fine.'''
print g0
Bin_initial = Bin_final
Bin_i=interval
No comments:
Post a Comment