Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python and MATLAB giving different results for same code?

  1. Feb 24, 2017 #1
    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
    Code (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
    Code (Matlab M):
    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: Feb 24, 2017
  2. jcsd
  3. Feb 24, 2017 #2

    DrClaude

    User Avatar

    Staff: Mentor

    You'll have to describe the differences you are seeing.
     
  4. Feb 24, 2017 #3
    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.
     
  5. Feb 24, 2017 #4

    DrClaude

    User Avatar

    Staff: Mentor

    These two lines are not equivalent:
    Code (Text):
    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. Feb 24, 2017 #5

    jack action

    User Avatar
    Science Advisor
    Gold Member

  7. Feb 24, 2017 #6
    Code (Text):


    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
     
  8. Feb 24, 2017 #7
    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.
     
  9. Feb 24, 2017 #8

    jack action

    User Avatar
    Science Advisor
    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:
    Code (Python):
    x=np.linspace(0,xsize,xnum)
    y=np.linspace(0,ysize,ynum)
    Code (Matlab M):
    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.
     
  10. Feb 24, 2017 #9

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    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
     
  11. Feb 24, 2017 #10
    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
    Code (Python):
            vx[i,j]= -vx0 * math.sin(math.pi * x[j]/xsize*2)  * math.cos(math.pi * y[i]/ysize)    
     
     
  12. Feb 25, 2017 #11

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    The syntax is exactly the same between python and matlab, use
    Code (Text):

    linspace(start value, final value, number of points)
     
     
  13. Feb 25, 2017 #12

    jtbell

    User Avatar

    Staff: Mentor

    How small is "very small"? Three significant figures? Five? Ten?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Python and MATLAB giving different results for same code?
  1. Matlab and Python (Replies: 10)

Loading...