lundi 29 janvier 2018

'if' condition doesn't work after second cycle [python]

I have a problem with if loop. In the script I need to check if difference between i and i+1 row is exual 0.6 and if yes, then the program should calculete grad[i] value. The problem is that in third circle, even if the condition is fulfiled, calculations grad[i] aren't performed. I feel in my bones that I made a stupid mistake but totally I don't know where. I would be very grateful for a clue/enlightenment. Best regards,

This is part of my code:

data = np.loadtxt(files[p])
N=len(data)
print "N: ", N, "\n"
for i in range(0,N-1):
    print "i: ", i
    j=i+1
    Fi[i]=data[i,1]
    errFi[i]=data[i,2]
    Fj[j]=data[j,1]
    errFj[j]=data[j,2]
    t1=data[i,0]
    t2=data[j,0]
    dt=(t2-t1)
    print "dt:", dt
    print Fj[j]-Fi[i]
    if dt == 0.6:
        print " dt =", dt
        if Fi[i] < Fj[j]:
            r[i]=1
            grad[i]=(Fj[j]-Fi[i]) / dt
        else:
            r[i]=-1
            grad[i]=(Fi[i]-Fj[j]) / dt

        print " grad[",i,"]=",grad[i] 

and the result in console is:

i:  0
dt: 0.6
0.148645
dt = 0.6
grad[ 0 ]= 0.247741666667

i:  1
dt: 0.6
0.061069
dt = 0.6
grad[ 1 ]= 0.101781666667

i:  2
dt: 0.6
-0.009578

i:  3
dt: 0.6
0.078995

i:  4
dt: 0.6
0.069982

Aucun commentaire:

Enregistrer un commentaire