Python and MATLAB giving different results for same code?

  • Context: MATLAB 
  • Thread starter Thread starter Atr cheema
  • Start date Start date
  • Tags Tags
    Code Matlab Python
Click For Summary

Discussion Overview

The discussion revolves around the differences in output between Python and MATLAB when translating a specific code snippet. Participants are examining the potential causes for discrepancies in results, particularly focusing on array indexing, function syntax, and numerical precision. The scope includes technical explanations and comparisons of programming practices in both languages.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant notes that the Python code produces slightly different results compared to the MATLAB code, which affects later calculations.
  • Another participant points out that the two lines of code in Python and MATLAB are not equivalent due to differences in parentheses placement.
  • It is mentioned that the Python range function outputs indices from 0 to 35, while MATLAB's for loop iterates from 1 to 36, raising questions about whether this affects the results.
  • Some participants suggest that the differences in results may stem from how the two languages handle numerical precision and rounding.
  • There is a discussion about the equivalence of the array generation methods in both languages, with a suggestion to compare the generated values directly.
  • One participant expresses uncertainty about the magnitude of the differences, asking for clarification on what "very small" means in terms of significant figures.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the exact cause of the differences in results. Multiple competing views remain regarding the impact of indexing, syntax, and numerical precision.

Contextual Notes

Participants have noted potential limitations in the code provided, such as the need for accurate parentheses placement and the handling of numerical precision in both programming environments. There are unresolved questions about the equivalence of the generated arrays and the significance of the differences observed.

Atr cheema
Messages
67
Reaction score
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:
Physics news on Phys.org
You'll have to describe the differences you are seeing.
 
DrClaude said:
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.
 
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.
 
DrClaude said:
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
 
jack action said:
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.
 
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.
 
Atr cheema said:
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
FactChecker said:
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
jack action said:
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
Atr cheema said:
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?
 

Similar threads

Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
17K
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K