[SciPy-user] Scilab to Scipy
Warren Weckesser
warren.weckesser@gmail....
Fri Aug 22 11:37:23 CDT 2008
Peter,
The problem is in your original scilab code. You say
x = linspace(0,100);
and in the calculation of the mode, you have the term sin(Kx*x). Since Kx =
2*pi, and your x ranges from 0 to 100, you should see 100 oscillations in
the x direction. You don't see it, because your grid is too coarse. The
default number of samples for the linspace function is 100.
What you really want is 0 <= x <= Lx. In your scilab code, x should be set
like this:
x = linspace(0,Lx,grid_size)
The third argument, as its name suggests, is the number of samples to use.
Here's a modified version of your scilab code (I also changed e^(%i*w*t) to
cos(w*t), but that was not the source of the problem):
####################
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;
grid_size = 501;
x = linspace(0,Lx,grid_size);
y = linspace(0,Ly,grid_size);
z = zeros(grid_size,grid_size);
for i = 1:grid_size
for j = 1:grid_size
z(i,j) = A * sin(Kx*x(i)) * sin(Ky*y(j)) * cos(w*t);
end
end
contour(x,y,z,20)
####################
Cheers,
Warren
On Fri, Aug 22, 2008 at 10:01 AM, peter websdell <
flyingdeckchair@googlemail.com> wrote:
> Hello all,
>
> I'm converting from using sclab/matlab to python. I'm finding it great so
> far, but I've discovered a problem that I don't understand.
>
> The following code displays a contour plot of the natural modes of a plate
> in 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(x,y,z,20)
> ##################
>
> Now here's how I've done it in python:
>
> ##################
> 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,y =mgrid[0:100,0:100]
> z=empty((100,100))
> z=A * sin(Kx*x) * sin(Ky*y) * e**(1j*w*t)
>
> contour(x,y,z)
> show()
> ###################
>
> The result does plot a contour, but it is garbage. I have tried replecating
> the silly for loop approach, and also using the real of abs value of the
> result, but it is still garbage.
>
> Can anyone offer some advice as to why the two scripts produce different
> results?
>
> Thanks a lot,
> Pete.
>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20080822/caf9a5f7/attachment.html
More information about the SciPy-user
mailing list