Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8132755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:25:10+00:00 2026-06-06T09:25:10+00:00

I am trying to convert the following code from Trefethen’s Spectral Methods in MATLAB

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:25:12+00:00Added an answer on June 6, 2026 at 9:25 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to convert the following code from msvc to gcc #define ltolower(ch)
i'm trying to convert the following code from Java to C#. // Replace 0
I'm trying to convert the following perl code: unpack(.., Z*) to python, however the
I'm trying to convert the following code from OpenGL 1.5 spec. to the OpenGLES
I'm trying to convert the following code from C# to Java, but I get
I am trying to convert the following LINQ code from C# to VB.NET. Here
I am trying to convert my date from NSString to NSDate using following code
I m trying to convert the following code to another AJAX call, in order
I am trying to convert the following two interfaces from a C header file
I was trying to convert a QString to char* type by the following methods,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.