# Python and MATLAB giving different results for same code?

• MATLAB
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:

DrClaude
Mentor
You'll have to describe the differences you are seeing.

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.

DrClaude
Mentor
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.

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

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.

jack action
Gold Member
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.

FactChecker
Gold Member
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

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)

Dr Transport
Gold Member
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)

jtbell
Mentor
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?