Python and MATLAB giving different results for same code?

  • #1
69
0
I am trying to learn Python by translating my MATLAB codes into Python. My translated Python code is giving slightly different result as compared to that of MATLAB. Although difference is very small but it affects largely the results at later stage.
My Python code is
Python:
import numpy as np
import math
import matplotlib.pyplot as plt
xsize=1000000
ysize=1500000
xnum=36
ynum=36
xstp=xsize/(xnum-1)
ystp=ysize/(ynum-1)
x=np.linspace(0,xsize,xnum)
y=np.linspace(0,ysize,ynum)
vx0=1e-9 * xsize/(2*ysize)
vy0=1e-9

vx=np.zeros((xnum,ynum))
vy=np.zeros((xnum,ynum))
dvxdx=np.zeros((xnum,ynum))
dvydy=np.zeros((xnum,ynum))
divv=np.zeros((xnum,ynum))

for i in range(ynum):
for j in range(xnum):
vx[i,j]=-vx0 * math.sin(math.pi*x[j]/xsize*2 * math.cos(math.pi*y[i])/ysize)

and my MATLAB code is
Matlab:
xsize=1000000;
ysize=1500000;

xnum=36;
ynum=36;

xstp=xsize/(xnum-1);
ystp=ysize/(ynum-1);

vx0=1e-9*xsize/2/ysize;
vy0=1e-9;


x=0:xstp:xsize;
y=0:ystp:ysize;


for i=1:1:ynum
    for j=1:1:xnum
       component distribution
        vx(i,j)=-vx0*sin(pi*x(j)/xsize*2)*cos(pi*y(i))/ysize);
  
    end
end
 
Last edited by a moderator:

Answers and Replies

  • #2
DrClaude
Mentor
7,578
3,932
You'll have to describe the differences you are seeing.
 
  • #3
69
0
You'll have to describe the differences you are seeing.
mean(mean(vx)) of MATLAB is different than that of Python. As said it is very small, but I want to understand why there is a difference.
 
  • #4
DrClaude
Mentor
7,578
3,932
These two lines are not equivalent:
Code:
vx[i,j]=-vx0 * math.sin(math.pi*x[j]/xsize*2 * math.cos(math.pi*y[i])/ysize)

vx(i,j)=-vx0*sin(pi*x(j)/xsize*2)*cos(pi*y(i))/ysize);
Note the placement of the parenthesis.

Actually, that second line is not valid, as the parenthesis don't balance out. If you do not provide us with the actual code, there is not much we can do to help you.
 
  • #6
69
0
These two lines are not equivalent:
Code:
vx[i,j]=-vx0 * math.sin(math.pi*x[j]/xsize*2 * math.cos(math.pi*y[i])/ysize)

vx(i,j)=-vx0*sin(pi*x(j)/xsize*2)*cos(pi*y(i))/ysize);
Note the placement of the parenthesis.

Actually, that second line is not valid, as the parenthesis don't balance out. If you do not provide us with the actual code, there is not much we can do to help you.
Code:
vx(i,j)=-vx0*sin(pi*x(j)/xsize*2)*cos(pi*y(i)/ysize);

Sorry I actually wrote the code, instead of copy pasting it. Here you see correct one
 
  • #7
69
0
Python range function outputs 0 <= i <= 35 and your Matlab for loop sets 1<= i <= 36.
I don't think this is the problem. If it were so then there must be difference in no. of elements of 'vx'. Also each of value of arrays from both codes are differing by very small fraction. range function of python is producting an array consisting of 36 elements through which for loop will iterate. For loop of MATLAB is explicitly iterating 36 times.
 
  • #8
jack action
Science Advisor
Insights Author
Gold Member
2,072
3,111
You are right, I looked quickly and thought you were actually using the index in your calculations. But I'm pretty sure it is related to this kind of problem.

How sure are you that the following gives the exact same results:
Python:
x=np.linspace(0,xsize,xnum)
y=np.linspace(0,ysize,ynum)
Matlab:
xstp=xsize/(xnum-1);
ystp=ysize/(ynum-1);

x=0:xstp:xsize;
y=0:ystp:ysize;

I know they should be, but I would compare them value for value to be sure, there may be some rounding that is done in different places. There is a good discussion on Stackoverflow on the many ways you can set a range in Python and the output differences.
 
  • #9
FactChecker
Science Advisor
Gold Member
6,190
2,399
Code:
vx(i,j)=-vx0*sin(pi*x(j)/xsize*2)*cos(pi*y(i)/ysize);

Sorry I actually wrote the code, instead of copy pasting it. Here you see correct one
As @DrClaude pointed out, the two equations are still not equivalent even with this correction.
The Python version takes the sin of math.pi*x[j]/xsize*2 * math.cos(math.pi*y)/ysize.
The MATLAB version only takes the sin of pi*x(j)/xsize*2
 
  • #10
69
0
As @DrClaude pointed out, the two equations are still not equivalent even with this correction.
The Python version takes the sin of math.pi*x[j]/xsize*2 * math.cos(math.pi*y)/ysize.
The MATLAB version only takes the sin of pi*x(j)/xsize*2
MATLAB Code inside for loops;
vx(i,j)=-vx0*sin(pi*x(j)/xsize*2)*cos(pi*y(i)/ysize);
Python Code inside for loops
Python:
        vx[i,j]= -vx0 * math.sin(math.pi * x[j]/xsize*2)  * math.cos(math.pi * y[i]/ysize)
 
  • #11
Dr Transport
Science Advisor
Gold Member
2,480
605
You are right, I looked quickly and thought you were actually using the index in your calculations. But I'm pretty sure it is related to this kind of problem.

How sure are you that the following gives the exact same results:
Python:
x=np.linspace(0,xsize,xnum)
y=np.linspace(0,ysize,ynum)
Matlab:
xstp=xsize/(xnum-1);
ystp=ysize/(ynum-1);

x=0:xstp:xsize;
y=0:ystp:ysize;

I know they should be, but I would compare them value for value to be sure, there may be some rounding that is done in different places. There is a good discussion on Stackoverflow on the many ways you can set a range in Python and the output differences.

The syntax is exactly the same between python and matlab, use
Code:
linspace(start value, final value, number of points)
 
  • #12
jtbell
Mentor
15,753
3,955
mean(mean(vx)) of MATLAB is different than that of Python. As said it is very small,
How small is "very small"? Three significant figures? Five? Ten?
 

Related Threads on Python and MATLAB giving different results for same code?

Replies
17
Views
2K
Replies
3
Views
7K
  • Last Post
Replies
10
Views
2K
Replies
5
Views
7K
Replies
3
Views
759
Replies
1
Views
628
Replies
0
Views
15K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
2
Views
1K
Top