Double summation - using Python

momo91

New member
Joined
Oct 16, 2020
Messages
9
Hello,

I have been trying to find the density of a fluid which is a function of concentration x and temperature t, the empirical correlations looks as this;

1614378850362.png

I tried to solve this using a simple Python code but I can not get it right, anyone that can see my mistake?


1614378887831.png

Kind regards,
/M
 
Don't know python - do i and j start from 0?
I know Python, and yes variable i will iterate through all the values from 0 up to (rows-1) inclusively. So it will take values {0,1,2} rather than {1,2,3}.
Maybe this answer illustrates why there's a problem with the code :) OP, do you understand?
 
Thank you very much :) Forgot that going from Matlab to Python.. So essentially it becomes X**(j+1-1) and t ** (j+1-1) = X**j and t**j
 
I made the change but still do not get it to work, have I missunderstood the double summation part maybe?
 
Then your loops iterate 4 times instead of 3.
No there go 0,1,2 and stop so i=j=2 at the final iteration; I can copy-paste the code


import numpy as np
A_1_1 = 1.0004 #i = 1, j=1
A_1_2 = 1.7659 * (10**-1) #i=1, j=2
A_1_3 = -4.9214 * (10**-2) # i=1, j=3
A_2_1 = -1.2379 * (10 **-4) # i=2,j=1
A_2_2 = -9.9189* (10**-4) # i=2, j=2
A_2_3 = 4.1024 * (10 **-4) # i=2, j=3
A_3_1 = -2.9837 * (10 **-6) #i=3,j=1
A_3_2 = 2.4614 * (10**-6) # i=3, j=2
A_3_3 = -9.5278 * (10**-8) # i=3, j=3

A = np.array([[A_1_1,A_1_2,A_1_3],[A_2_1,A_2_2,A_2_3],[A_3_1,A_3_2,A_3_3]])

rows, cols = A.shape
density = 0
X=0.75
t=155
SUM = 0
for i in range(0, rows):
for j in range(0, cols):
SUM += A[j] * X**j * t**j
 
No there go 0,1,2 and stop so i=j=2 at the final iteration; I can copy-paste the code


import numpy as np
A_1_1 = 1.0004 #i = 1, j=1
A_1_2 = 1.7659 * (10**-1) #i=1, j=2
A_1_3 = -4.9214 * (10**-2) # i=1, j=3
A_2_1 = -1.2379 * (10 **-4) # i=2,j=1
A_2_2 = -9.9189* (10**-4) # i=2, j=2
A_2_3 = 4.1024 * (10 **-4) # i=2, j=3
A_3_1 = -2.9837 * (10 **-6) #i=3,j=1
A_3_2 = 2.4614 * (10**-6) # i=3, j=2
A_3_3 = -9.5278 * (10**-8) # i=3, j=3

A = np.array([[A_1_1,A_1_2,A_1_3],[A_2_1,A_2_2,A_2_3],[A_3_1,A_3_2,A_3_3]])

rows, cols = A.shape
density = 0
X=0.75
t=155
SUM = 0
for i in range(0, rows):
for j in range(0, cols):
SUM += A[j] * X**j * t**j
Sorry, should've looked up the syntax.
When you say it doesn't work, what exactly do you mean? Wrong result is produced?
 
Sorry, should've looked up the syntax.
When you say it doesn't work, what exactly do you mean? Wrong result is produced?
Yes I get the wrong results, so it might be that I am calculating/using the correlation wrong
 
Yes I get the wrong results, so it might be that I am calculating/using the correlation wrong
Can you use a debugger? If not - replace all values by something easy to deal with: 0 or 1. I would start with 1 for x and t and a bunch of 0s and 1s in the matrix so you could easily calculate the result. If you get what's expected start gradually using actual values.
 
Can you use a debugger? If not - replace all values by something easy to deal with: 0 or 1. I would start with 1 for x and t and a bunch of 0s and 1s in the matrix so you could easily calculate the result. If you get what's expected start gradually using actual values.
I tried that. I think there is something wrong with the correlation that have been published. I get right values for low temperatures (t) /concentrations (X) but very wrong when they are chosen in the higher part of the allowed interval in which the correlation should be valid. Very strange!
 
The quoted equation (repeated below) does seem very strange.

[math] \rho = \sum_{i=1}^{3}{\sum_{j=1}^{3}{A_{i,j}\cdot x^{\left(j-1\right)}\cdot t^{\left(j-1\right)}}} [/math]
It seems to me that there might be a typo. Perhaps one of the j terms within the exponents ought to be an i. If the above equation is correct then it would be better presented in the following equivalent, factored, form...

[math] \rho = \sum_{j=1}^{3}{ \left( \sum_{i=1}^{3}{A_{i,j} }\right) \cdot x^{\left(j-1\right)}\cdot t^{\left(j-1\right)}} [/math]
But without extra context I can't be sure.
 
Top