0

I'm trying to convert a code from MATLAB to Python line by line, and I'm getting different results for some reason.

This is the code from MATLAB:

T = 150.00;
P = 3200.00;
P = P*6.894757*0.001;

w(1,1) = 1402.85; w(1,3) = 3.437*10^(-3); % Table 2
w(2,1) = 4.871; w(2,3) = 1.739*10^(-4);
w(3,1) = -0.04783; w(3,3) = -2.135*10^(-6);
w(4,1) = 1.487*10^(-4); w(4,3) = -1.455*10^(-8);
w(5,1) = -2.197*10^(-7); w(5,3) = 5.230*10^(-11);
w(1,2) = 1.524; w(1,4) = -1.197*10^(-5);
w(2,2) = -0.0111; w(2,4) = -1.628*10^(-6);
w(3,2) = 2.747*10^(-4); w(3,4) = 1.237*10^(-8);
w(4,2) = -6.503*10^(-7); w(4,4) = 1.327*10^(-10);
w(5,2) = 7.987*10^(-10); w(5,4) = -4.614*10^(-13);

sum = 0;
for i=1:5
  for j=1:4
    sum = sum+w(i,j)*T^(i-1)*P^(j-1);
  end
end
v_water = sum

The answer to that would be v_water = 1.5242e+03.
However, when I try to convert it to Python with the following syntax:

T = 150.00;
P = 3200.00;
P = P*6.894757*0.001;

w = np.zeros(shape = (5,4))
w[0,0]=1402.85
w[0,2]=np.dot(3.437,10 ** (- 3))
w[1,0]=4.871
w[1,2]=np.dot(1.739,10 ** (- 4))
w[2,0]=- 0.04783
w[2,2]=np.dot(- 2.135,10 ** (- 6))
w[3,0]=np.dot(1.487,10 ** (- 4))
w[3,2]=np.dot(- 1.455,10 ** (- 8))
w[4,0]=np.dot(- 2.197,10 ** (- 7))
w[4,2]=np.dot(5.23,10 ** (- 11))
w[0,1]=1.524
w[0,3]=np.dot(- 1.197,10 ** (- 5))
w[1,1]=- 0.0111
w[1,3]=np.dot(- 1.628,10 ** (- 6))
w[2,1]=np.dot(2.747,10 ** (- 4))
w[2,3]=np.dot(1.237,10 ** (- 8))
w[3,1]=np.dot(- 6.503,10 ** (- 7))
w[3,3]=np.dot(1.327,10 ** (- 10))
w[4,1]=np.dot(7.987,10 ** (- 10))
w[4,3]=np.dot(- 4.614,10 ** (- 13))

sum=0
for i in range(5):
    for j in range(4):
        sum=sum + w[i,j]*T**(i-1)*P**(j-1)
v_water = sum

I'm getting v_water = 0.4605639858054064. What am I doing wrong in the Python code?

2
  • What about T and P? Commented Nov 14, 2021 at 6:17
  • Sorry, T and P declaration remains the same Commented Nov 14, 2021 at 6:20

1 Answer 1

1

In MATLAB, the i range is 1,...,5 and the j range is 1,...,4

In Python, the i range is 0,...,4 and the j range is 0,...,3

Yet in both code you use (i-1) and (j-1) for the power. To get the same result you need to adjust the Python power up 1. I.e., use i and j instead of (i-1) and (j-1).

Sign up to request clarification or add additional context in comments.

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.