Python and MATLAB giving different results for same code?

In summary: What is the actual difference in numerical value between the two versions? In summary, the conversation discusses a problem with translating MATLAB codes into Python and obtaining slightly different results. The difference is small but it affects the results at a later stage. The main issue is related to the placement of parentheses and the calculation of sin in the equations used in the code. Both users suggest comparing the values outputted by the code to ensure they are the same.
  • #1
Atr cheema
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:
Physics news on Phys.org
  • #2
You'll have to describe the differences you are seeing.
 
  • #3
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.
 
  • #4
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
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
 
  • #7
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.
 
  • #8
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
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?
 

1. Why do Python and MATLAB give different results for the same code?

The main reason for this is that Python and MATLAB are two different programming languages with their own unique syntax and ways of handling data. Therefore, even if the code appears to be the same, the underlying logic and execution may differ, leading to different results.

2. Is one language more accurate than the other?

Neither language is inherently more accurate than the other. They both have their own strengths and weaknesses, and the accuracy of the results depends on the specific implementation and use case.

3. How can I ensure consistent results between Python and MATLAB?

One way to ensure consistent results is to thoroughly test and debug your code in both languages. It is also important to understand the differences in the languages and how they handle data and perform calculations.

4. Can I convert my code from one language to the other?

While it is possible to convert code from one language to another, it may not always be straightforward. It is important to carefully review and test the converted code to ensure accuracy.

5. Are there any resources available for troubleshooting differences in results between Python and MATLAB?

Yes, there are many online forums and communities dedicated to helping users troubleshoot and understand differences in results between Python and MATLAB. It can also be helpful to consult the official documentation for both languages.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
898
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • Programming and Computer Science
Replies
2
Views
16K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
4
Views
2K
Back
Top