嗨,我正在尝试将以下代码从 Trefethen 在 MATLAB 中的光谱方法转换为 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
除了这行代码外,大部分情况下进展顺利
data = [v; zeros(nplots,N)]
在阅读了如何在 Numpy 和 Matlab 之间进行转换http://www.scipy.org/NumPy_for_Matlab_Users#head-e9a492daa18afcd86e84e07cd2824a9b1b651935我尝试通过执行以下操作来转换它
data = np.array(v,zeros(nplots,N))
但我得到这个错误
data = np.array(v,zeros(nplots,N));
TypeError: data type not understood
我认为这是因为一个 numpy arrry 具有这种结构
numpy.array(object,dtype=none)
我将不胜感激转换该行的任何帮助。先感谢您!