- #1

- 69

- 0

## Main Question or Discussion Point

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

and my MATLAB code is

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)
```

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: