# Help with converting from scilab

Hello all, my first post here.

I am moving from using scilab to Pylab, can anyone tell me why the two following snippets of code produce very different results? BTW. The scilab code produces the expected result.

Scilab:

Lx=1;
Ly=1;
n=2;
m=2;
f=100;
w=2*%pif;
t=1;
A=2;
Kx=n
%pi/Lx;
Ky=m*%pi/Ly;
x=linspace(0,100);
y=linspace(0,100);
z=zeros(100,100);
for i = 1:100
for j = 1:100
z(i,j) = A * sin(Kxx(i)) * sin(Kyy(j)) * %e^(%iwt);
end
end
contour(z)

Pylab:

from pylab import *
Lx=1
Ly=1
n=2
m=2
f=100
w=2pif
t=1
A=2
Kx=npi/Lx
Ky=m
pi/Ly
x=arange(0,100)
y=arange(0,100)
z=empty((100,100))
for i in range(1,100):
for j in range(1,100):
z[i, j] = A * sin(Kxx[i]) * sin(Kyy[j]) * e**(1jwt)
contourf(z)
show()

Cheers,

Pete.

Hello all, my first post here.

I am moving from using scilab to Pylab, can anyone tell me why the two
following snippets of code produce very different results? BTW. The scilab
code produces the expected result.

I don't know what scilab does, but the filling of the z array looks
wrong, since python indexing starts at 0, not 1, so you would want to
do

for i in range(0,100):
for j in range(0,100):

but you rarely want to loop over arrays. My snippet below shows how
to use meshgrid to use numpy's elementwise operations and avoid loops.
By only filling starting at 1, you are using the memory in the 0-th
row and column unintialized (np.zeros can be safer than np.empty in
this regard) Also, be careful when defining constants as integers,
since integer division produces integers (3/2=1)

Here is my script - does it produce what you are expecting?

import numpy as np
import matplotlib.pyplot as plt

Lx = 1.
Ly = 1.
n = 2.
m = 2.
f = 100.
w = 2*np.pi*f
t = 1.
A = 2.
Kx = n*np.pi/Lx
Ky = m*np.pi/Ly
x = np.arange(100.)
y = np.arange(100.)
X, Y = np.meshgrid(x, y)
Z = A * np.sin(Kx*X) * np.sin(Ky*Y) * np.exp(1j*w*t)
plt.contourf(Z)
plt.show()

···

On Fri, Jul 25, 2008 at 10:22 AM, Peter Wesbdell <flyingdeckchair@...982...> wrote:

Scilab:
Lx=1;
Ly=1;
n=2;
m=2;
f=100;
w=2*%pi*f;
t=1;
A=2;
Kx=n*%pi/Lx;
Ky=m*%pi/Ly;
x=linspace(0,100);
y=linspace(0,100);
z=zeros(100,100);
for i = 1:100
for j = 1:100
z(i,j) = A * sin(Kx*x(i)) * sin(Ky*y(j)) * %e^(%i*w*t);
end
end
contour(z)

Pylab:
from pylab import *
Lx=1
Ly=1
n=2
m=2
f=100
w=2*pi*f
t=1
A=2
Kx=n*pi/Lx
Ky=m*pi/Ly
x=arange(0,100)
y=arange(0,100)
z=empty((100,100))
for i in range(1,100):
for j in range(1,100):
z[i, j] = A * sin(Kx*x[i]) * sin(Ky*y[j]) * e**(1j*w*t)
contourf(z)
show()

Cheers,
Pete.
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great
prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users