jeudi 17 octobre 2019

For loop from matlab to python - different results

I'm trying to rewrite code from matlab to python. I have problem with for loop, because result of h are different.

here is the original matlab code

for iter = 1:n-1
    j=plnn; % previous line node-right running node
    h=clnn; % current node at center line
    theta(h)=0;
    SR(h)=SR(j);
    nu(h)=SR(j)-theta(h);
    SL(h)=theta(h)-nu(h);
    j=j+1; % increasing node number in previous line

    fprintf('h',h);
    disp(h);
    fprintf('j',j);
    disp(j);

    for i=h+1:n-plnn+clnn % increaasing current node number up to just previous from wall at current characteristic line
        SR(i)=SR(j);
        SL(i)=SL(i-1);
        theta(i)=0.5*(SR(i)+SL(i));
        nu(i)=0.5*(SR(i)-SL(i));
        j=j+1;
    end
    if i==n-plnn+clnn % if (i) equals to node which is just previous one from the wall
        h=i+1; % increase current node number to the wall node
    else
        h=h+1;
    end
    theta(h)=theta(h-1); % node at wall is solving
    nu(h)=nu(h-1);
    SL(h)=SL(h-1);
    SR(h)=SR(h-1);
    plnn=plnn+1;
    clnn=h+1;
end

results of h and j:

h    32 j     3 h    62 j     4 h    91 j     5 h   119 j     6 h   146 j     7 h   172 j     8 h   197 j     9 h   221 j    10 h   244 j    11 h   266 j    12 h   287 j    13 h   307 j    14 h   326 j    15 h   344 j    16 h   361 j    17 h   377 j    18 h   392 j    19 h   406 j    20 h   419 j    21 h   431 j    22 h   442 j    23 h   452 j    24 h   461 j    25 h   469 j    26 h   476 j   
27 h   482 j    28 h   487 j    29 h   491 j    30 h   494 j    31

and here is that what i have already done in python

for i in range(n-1):
    j=plnn #previous line node-right running node
    h=clnn #current node at center line
    theta[h]=0
    SR[h]=SR[j]
    nu[h]=SR[j]-theta[h]
    SL[h]=theta[h]-nu[h]
    j=j+1 #increasing node number in previous line

    print('h',h)
    print('j',j)

    for i in range(h+1,n-plnn+clnn): #increaasing current node number up to just previous from wall at current characteristic line
        SR[i]=SR[j]
        SL[i]=SL[i-1]
        theta[i]=0.5*(SR[i]+SL[i])
        nu[i]=0.5*(SR[i]-SL[i])
        j=j+1

    if i==n-plnn+clnn: #if (i) equals to node which is just previous one from the wall
        h=i+1  #increase current node number to the wall node
    else:
        h=h+1

    theta[h]=theta[h-1] #node at wall is solving
    nu[h]=nu[h-1]
    SL[h]=SL[h-1]
    SR[h]=SR[h-1]
    plnn=plnn+1
    clnn=h+1

and results of its:

h 32 j 3 h 34 j 4 h 36 j 5 h 38 j 6 h 40 j 7 h 42 j 8 h 44 j 9 h 46 j 10 h 48 j 11 h 50 j 12 h 52 j 13 h 54 j 14 h 56 j 15 h 58 j 16 h 60 j 17 h 62 j 18 
h 64 j 19 h 66 j 20 h 68 j 21 h 70 j 22 h 72 j 23 h 74 j 24 h 76 j 25 h 78 j 26 h 80 j 27 h 82 j 28 h 84 j 29 h 86 j 30 h 88 j 31

As you can see, the h in python increase gradually by 2 vaule, but j in both are the same.

Aucun commentaire:

Enregistrer un commentaire