0

我正在尝试将以下代码从 Trefethen 在 MATLAB 中的光谱方法转换为 Python。但是遇到以下关于索引越界的错误。对于什么索引超出范围以及如何修复它,我有点困惑。任何帮助,将不胜感激。

错误

Traceback (most recent call last):
  File "C:\Documents and Settings\My Documents\Computational Physics\Wave-eqn.py", line 56, in <module>
ax.plot_wireframe(x,tdata,data,rstride=10, cstride=10)
  File "C:\Python32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 906, in  plot_wireframe
    tylines = [tY[i] for i in cii]
  File "C:\Python32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 906, in  <listcomp>
    tylines = [tY[i] for i in cii]
IndexError: index out of bounds

Trefethen 的代码

% p6.m - variable coefficient wave equation using differentiation matrices

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

column = [0 .5*(-1).^(1:N-1).*cot((1:N-1)*h/2)];
D = toeplitz(column,-column);

% Time-stepping by leap frog formula:
tmax = 15; 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));
        w = (D*v')';
        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 3]), ylabel t, zlabel u, grid off

我的代码

import numpy as np
from numpy import *
from math import  pi
from scipy.linalg import toeplitz
from scipy.special import cotdg
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt


N = 512
h = (2*np.pi)/N
x = h*(np.arange(N)+1)
t = 0
dt = h/4.
a = .1
c = a + np.sin(x - 1)**2
v = np.exp(-100 * (x - 1)**2)
vold = np.exp(-100 * (x - a*dt - 1)**2)

column = ((0.5*(-1)**arange(N))*cotdg(arange(N))*(h/2));
D = toeplitz(column,-column);
#print(D.shape);


tmax = 15   
tplot = .15
plotgap = int(around(tplot/dt));print(plotgap)
dt = tplot/plotgap
nplots = int(round((tmax/tplot)));print(nplots)
k = np.zeros(((nplots,N))) 
data = np.concatenate((v.reshape((512,1)).transpose(), k))
tdata = t

for i in range(nplots):
    for n in range(plotgap):
        t = t+dt
        w = (D*v)
        vnew = vold-2*dt*c*w
        vold = v
        v = vnew
    data[i,:] = v[0,:]
    tdata = vstack([tdata, t])
print('shape data =',data.shape)
print('shape v =',v.shape)
print('shape tdata =',tdata.shape)
print('shape x =',x.shape)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(x,tdata,data,rstride=10, cstride=10)
plt.show()

由python完成的数组形状

 shape data = (101, 512)
 shape tdata = (101, 1)
 shape x = (512,)

我有一个朋友在 MatLab 中为这段代码运行 size() 命令,他为数组想出了这些形状

data =  101 512
tdata = 101 1
x =     1   512
4

2 回答 2

1

的前两个参数plot_wireframe需要是二维数组。文档

我不确切知道如何自定义您的代码(因为代码很多),但我希望这会有所帮助。

编辑:尝试axes3d.get_test_data查看有效输入应该是什么样子的示例。

于 2012-08-01T20:39:22.037 回答
0

在 matplotlib-users@lists.sourceforge.net 的人们的帮助下,我想通了。数组

x and tdata

需要广播所以我做了

X,Y = np.broadcast_arrays(x,tdata)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X,Y,data,rstride=5, cstride=5)
plt.show()
于 2012-08-03T04:35:29.830 回答