1

I have written a code that I want to use to reduce measurement data. For this, I iterate through 30 sets of measurement data. In each iteration, I use fsolve to solve a set of three non-linear equations. This give me an array containing three values that are then further processed (in the example below lbda, alp, bta, dlt, q, N). I can print the results but would need to collect the data for all of the 30 cycles in an 30 by 6 array to do some statistics on (i.e. np.mean on each of the 6 variables).

I've tried the, for me, most obvious function(s) np.append, np.vstack, np.concatenates at the end of each iteration but this only gives me a 1 by 6 array containing only the last iteration step rather than the desired array containing all of the 30 iteration steps.


# loading data above 

m1 = data_arr_blkcorr [:,4] / data_arr_blkcorr [:,2]
m2 = data_arr_blkcorr [:,5] / data_arr_blkcorr [:,2] 
m3 = data_arr_blkcorr [:,7] / data_arr_blkcorr [:,2]


N=-1
while (N<29):

    N = N+1 

    T1 = 79.744440299369400 
    T2 = 4.756431967877120 
    T3 = 195.146815878103000 
    T4 = 1.333609171398 
    T5 = 0.540566631391 
    T6 = 1 
    T7 = 1.731261585620 

    T_all = np.array([T4, T5, T6, T7, T1, T2, T3])

    n1 = 0.598169735 
    n2 = 1.509919737 
    n3 = 0.600477235 
    n4 = 0.9364071191658 
    n5 = 0.5815716133216 
    n6 = 1 
    n7 = 1.0455228260642 

    n_all = np.array([n4, n5, n6, n7, n1, n2, n3])


    I1 = 94.905838
    I2 = 96.906018
    I3 = 97.905405
    I4 = 99.907473

    I5 = 91.90681
    I6 = 93.90509
    I7 = 95.90468

# some definition of variables here

    A11 = T1-n1
    A12 = T2-n2
    A13 = T3-n3

    A21 = -n1*P1
    A22 = -n2*P2
    A23 = -n3*P3

    A31 = m1[N] * P1
    A32 = m2[N] * P2
    A33 = m3[N] * P3

    b11 = m1[N] - n1
    b12 = m2[N] - n2
    b13 = m3[N] - n3

# some definition of variables here

    T = np.array ([T1, T2, T3])
    n = np. array([n1, n2, n3])
    m = np.array([m1[N], m2[N], m3[N]])
    P = np.array([P1, P2, P3])


    def F(x):
        return x[0]*T + (1-x[0])*n*np.exp(-x[1]/(1-x[0])*P) - m*np.exp(-x[2]*P) 

    y = fsolve(F, guess)

    lbda = y[0]
    alp = y[1]/(1-y[0])
    bta = y[2]

    dlt = (np.exp(-alp*P2)-1)*1000
    N_all = n_all * np.exp(-alp*P_all)

    q = (1 + (1 - lbda) / lbda * np.sum(N_all) / np.sum(T_all))**(-1)

    print (lbda, alp, bta, dlt, q, N) 

Going through the posts I have also used this (after a suggestion provided by Koke Cacao):

data_sum = None
new_data = [lbda, alp, bta, dlt, q, N]
data_sum = np.append([data_sum], new_data) if data_sum is not None else new_data
print(data_sum)

But this yields a list of 30 isolated 1 by 6 arrays to which I do not have access to on the whole (i.e. to calculate np.means for individual values over all 30 iteration steps).

0.0209809690838 0.00142553246898 1.61537217874 -0.0443566490317 0.492710128581 26
(0.020980969083774538, 0.0014255324689812997, 1.6153721787428821, -0.044356649031684903, 0.4927101285811698, 26)
0.0209791772348 0.00272489389093 1.61486845411 -0.0847856651612 0.492691689834 27
(0.020979177234773643, 0.0027248938909269797, 1.6148684541135419, -0.084785665161235535, 0.49269168983354455, 27)
0.0209792771323 0.004884280445 1.61191395635 -0.151970341101 0.49269849879 28
(0.020979277132325381, 0.0048842804449965851, 1.6119139563515672, -0.15197034110059349, 0.4926984987899769, 28)
0.0209799414614 0.00256323393277 1.61366560195 -0.0797557810515 0.492700571038 29
(0.020979941461433328, 0.0025632339327746521, 1.6136656019498359, -0.079755781051460417, 0.49270057103806092, 29)

Also, it concatenates results over multiple runs (even after shutting down Python and restarting it) and I cannot clear this (sort of) memory.

2
  • what do you want to do exactly? Commented Aug 4, 2019 at 8:17
  • where do you create list/array for all data? Commented Aug 4, 2019 at 8:26

2 Answers 2

1

try creating an empty list outside your while loop and then append the array.

solution = []
while n < 29:
    #your code here
    solution.append([lbda, alp, bta, dlt, q, N])
Sign up to request clarification or add additional context in comments.

2 Comments

Tell me if it doesn't work and what output you're getting. We'll try something else. But it should work anyways.
Please don't forget to mark this as the solution if it worked for ya! Cheers.
0

you should declare an empty list outside the while loop scope and then append to it on each Iteration:

result = []
while(N<29):
   # calculate something
   result.append(your_data)

print(result)   # that will give you all the data that you got from each Iteration

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.