1

I am trying to convert the following code from Trefethen's Spectral Methods in MATLAB to Python.

% p6.m - variable coefficient wave equation

% Grid, variable coefficient, and initial data:
  N = 128; h = 2*pi/N; x = h*(1:N); t = 0; dt = h/4;
  c = .2 + sin(x-1).^2;
  v = exp(-100*(x-1).^2); vold = exp(-100*(x-.2*dt-1).^2);

% Time-stepping by leap frog formula:
  tmax = 8; tplot = .15; clf, drawnow, set(gcf,'renderer','zbuffer')
  plotgap = round(tplot/dt); dt = tplot/plotgap;
  nplots = round(tmax/tplot);
  data = [v; zeros(nplots,N)]; tdata = t;
  for i = 1:nplots
    for n = 1:plotgap
      t = t+dt;
      v_hat = fft(v);
      w_hat = 1i*[0:N/2-1 0 -N/2+1:-1] .* v_hat;
      w = real(ifft(w_hat)); 
      vnew = vold - 2*dt*c.*w; vold = v; v = vnew;
    end
    data(i+1,:) = v; tdata = [tdata; t];
  end
  waterfall(x,tdata,data), view(10,70), colormap(1e-6*[1 1 1]);
  axis([0 2*pi 0 tmax 0 5]), ylabel t, zlabel u, grid off

For the most part it is going smoothly except for this line of code

 data = [v; zeros(nplots,N)]

After reading how to convert between Numpy and Matlab here Link I tried to convert it by doing the following

data = np.array(v,zeros(nplots,N))

but I get this error

 data = np.array(v,zeros(nplots,N));
 TypeError: data type not understood

Which I assume is because a numpy array has this structure

  numpy.array(object,dtype=none)

I would appreciate any help with converting that line. Thank you in advance!

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Stripers247
  • 2,265
  • 11
  • 38
  • 40
  • maybe `np.vstack((v, zeros(nplots, N)))` – JBernardo Jun 25 '12 at 23:06
  • @JBernardo thanks for the reply. i tried the vstack, but I still get the `TypeError: data type not understood` error – Stripers247 Jun 25 '12 at 23:32
  • Check the number of parens... There must be to more than the *usual* :) – JBernardo Jun 25 '12 at 23:46
  • 1
    Related: http://stackoverflow.com/questions/9845292/converting-matlab-to-python?rq=1, http://stackoverflow.com/questions/2326786/matlab-to-python-code-conversion-numpy-scipy-matplotlib?rq=1 – 0 _ Aug 26 '13 at 04:31

1 Answers1

2

data = [v; zeros(nplots,N)] this is concatenating two matrices and stacken them up, note the ; in numpy you can use numpy.concatenate((v, zeros((nplots,N))), axis = 0) where axis is by which axis you want to concatenate by ...

data = np.array(v,zeros(nplots,N));
TypeError: data type not understood

basically when you call np.array the fist argument must be iterable object, list, tuple and on the second argument must be the type ie 'int', 'float32', 'float32' and so on ... but you set the type to zeros(nplots,N) numpy is complaining that it isn't a type ... numpy.zeros is the same the first argument must be a tuple and the second the type, sorry about that I didn't properly include ()

it should be data = numpy.concatenate((v, numpy.zeros((nplots,N))), axis = 0) assuming that you want to use double type which is the standard type.

Samy Vilar
  • 10,800
  • 2
  • 39
  • 34