次のコードを MATLAB の Trefethen の Spectral Methods から 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
トレフェセンのコード
% 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