Home > Net >  After converting matlab code to python, I'm getting different results
After converting matlab code to python, I'm getting different results

Time:11-14

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 Any help would be appreciated

CodePudding user response:

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).

  • Related