Python For循环索引问题
感谢大家最近帮助我学会Python的错误。下面我发布了一个Python代码,假设该代码可以为波传播设置动画Python For循环索引问题,python,matlab,indexing,Python,Matlab,Indexing,感谢大家最近帮助我学会Python的错误。下面我发布了一个Python代码,假设该代码可以为波传播设置动画 import numpy as np import matplotlib.pyplot as plt step = 0.1 deltax = 0.1 step1 = 0.2 deltax1 = 0.1 step2 = 0.2 deltax2 = 0.2 M = 100 c = 1 r = (c*step)/float(deltax) r1 = c*step1 / float(delta
import numpy as np
import matplotlib.pyplot as plt
step = 0.1
deltax = 0.1
step1 = 0.2
deltax1 = 0.1
step2 = 0.2
deltax2 = 0.2
M = 100
c = 1
r = (c*step)/float(deltax)
r1 = c*step1 / float(deltax1)
r2 = c*step2 / float(deltax2)
N = 4
k = 1000.0
x0 = 0.3
x = np.arange(1/float(M),1,1/float(M))
x = np.transpose(x)
y1 = np.exp(-k*((x-x0)**2))
y2 = np.exp(-k*((x-x0)**2))
y3 = np.exp(-k*((x-x0)**2))
ycurrent = np.zeros(shape=(1,99))
ycurrent1 = np.zeros(shape=(1,99))
ycurrent2 = np.zeros(shape=(1,99))
xdisp = np.arange(1,M,1)
counter = 1
for n in np.arange(1,50,1):
for i in np.arange(1,M,1):
ycurrent[i] = 2*(1-r**2)*y1[i]-y1[i]+(r**2)*(y1[i+1]+y1[i-1])
plt.plot(xdisp/float(M),ycurrent)
plt.pause(.1)
使用此代码,我得到一个错误,即
line 46, in <module>
ycurrent[i] = 2*(1-r**2)*y1[i]-y1[i]+(r**2)*(y1[i+1]+y1[i-1])
IndexError: index 1 is out of bounds for axis 0 with size 1
第46行,在
Y电流[i]=2*(1-r**2)*y1[i]-y1[i]+(r**2)*(y1[i+1]+y1[i-1])
索引器:索引1超出大小为1的轴0的界限
我觉得这似乎与我使用np.zero的方式有关,但我以前就错了。我以前在stackexchange上看到过有人发布过关于此错误的帖子,但我已经找到了答案,我认为这个问题是由数组之间的维度问题引起的
下面是我用作参考的MatLab代码
%For r > 1 change step to .2;
%For r < 1 put step to .1 and make deltax .2;
step=.1; %Initialize delta t.
deltax=.1; %Initialize delta x.
step1=.2;%Initialize delta t1.
deltax1=.1;%Initialize delta x1.
step2=.1; %Initialize delta t2.
deltax2=.2;%Initialize delta x2.
M=100;
c=1;
r=((c*step)/deltax); %Initialize r.
r1=((c*step1)/deltax1);%Initialize r1
r2=((c*step2)/deltax2);%Initialize r2
k=1000; %Initialize a matrix in m^-2
x0=.3; %Initialize x initial in meters.
x=1/M:1/M:1;
y0=exp(-k.*((x-x0).^2)); % Equation for y initial.
N=4; %This will be the time on the string.
y=y0; % We initialize y which will be used later to calculate position.
y1=y0;
y2=y0;
ycurrent=zeros(1,M);
ycurrent1=zeros(1,M);
ycurrent2=zeros(1,M);
xdisp=1:1:M;
yprev=y0;
yprev1=y0;
yprev2=y0;
for n=1:100 %We want to loop the propogation function 100 times.
for i = 2:M-1;
ycurrent(i)= 2*(1-r.^2)*y(i)-yprev(i)+(r.^2)*(y(i+1)+y(i-1));
ycurrent1(i)= 2*(1-r1.^2)*y1(i)-yprev1(i)+(r1.^2)*(y1(i+1)+y1(i-1));
ycurrent2(i)= 2*(1-r2.^2)*y2(i)-yprev2(i)+(r2.^2)*(y2(i+1)+y2(i-1));
end
yprev=y;
y=ycurrent;
yprev1=y1;
y1=ycurrent1;
yprev2=y2;
y2=ycurrent2;
%Now we want to plot our wave using subplots so that they are all on the same plot.
figure(1)
subplot(2,2,1)
plot(xdisp/M,y,'r')%This creates our plot.
axis([0 1 -1.2 1.2]); %This is our axis for the plot.
xlabel('position on wave')
ylabel('Displacement')
title('Progagation of Wave r = 1')
legend('wave r = 1')
subplot(2,2,2)
plot(xdisp/M,y1,'r')
axis([0 1 -1.2 1.2]);
xlabel('position on wave')
ylabel('Displacement')
title('Progagation of Wave r > 1')
legend('Wave r > 1')
subplot(2,2,3)
plot(xdisp/M,y2,'r')
axis([0 1 -1.2 1.2]);
xlabel('position on wave')
ylabel('Displacement')
title('Progagation of Wave')
legend('Wave r < 1')
pause(.1)
end
%r>1将步骤更改为.2;
%对于r<1,将步骤设置为.1,并将deltax设置为.2;
步骤=.1;%初始化delta t。
deltax=.1;%初始化delta x。
步骤1=.2;%初始化增量t1。
deltax1=.1;%初始化delta x1。
步骤2=.1;%初始化增量t2。
deltax2=.2;%初始化delta x2。
M=100;
c=1;
r=((c*步骤)/deltax);%初始化r。
r1=((c*step1)/deltax1);%初始化r1
r2=((c*step2)/deltax2);%初始化r2
k=1000;%在m^-2中初始化矩阵
x0=.3;%初始化x首字母,单位为米。
x=1/M:1/M:1;
y0=exp(-k.*((x-x0)。^2));%y初始值的方程。
N=4;%这将是字符串上的时间。
y=y0;%我们初始化y,稍后将使用它来计算位置。
y1=y0;
y2=y0;
Y电流=零(1,M);
Y电流1=零(1,M);
ycurrent2=零(1,M);
xdisp=1:1:M;
yprev=y0;
yprev1=y0;
yprev2=y0;
对于n=1:100%,我们希望将传播函数循环100次。
对于i=2:M-1;
ycurrent(i)=2*(1-r.^2)*y(i)-yprev(i)+(r.^2)*(y(i+1)+y(i-1));
ycurrent1(i)=2*(1-r1.^2)*y1(i)-yprev1(i)+(r1.^2)*(y1(i+1)+y1(i-1));
ycurrent2(i)=2*(1-r2.^2)*y2(i)-yprev2(i)+(r2.^2)*(y2(i+1)+y2(i-1));
结束
yprev=y;
y=y电流;
yprev1=y1;
y1=Y电流1;
yprev2=y2;
y2=y电流2;
%现在我们想用子图绘制波,这样它们都在同一个图上。
图(1)
子地块(2,2,1)
绘图(xdisp/M,y,'r')%这将创建我们的绘图。
轴([01-1.21.2]);%这是我们的情节轴心。
xlabel(“波上的位置”)
ylabel(‘位移’)
标题(‘波r=1’的前进)
图例(“波r=1”)
子地块(2,2,2)
绘图(xdisp/M,y1,'r')
轴([01-1.21.2]);
xlabel(“波上的位置”)
ylabel(‘位移’)
标题(‘波r>1’的推进)
图例('Wave r>1')
子地块(2,2,3)
绘图(xdisp/M,y2,'r')
轴([01-1.21.2]);
xlabel(“波上的位置”)
ylabel(‘位移’)
标题(“波浪前进”)
图例(“波r<1”)
暂停(.1)
结束
Jayanth Koushik是正确的
在Python中,第一个索引是0,而不是1。您可能会发现以下内容很有帮助:
请将完整回溯添加到您的帖子中。它包括行号之类的内容。@MorganThrapp刚刚添加了它。我认为主要的问题是您直接将matlab代码翻译成python。但两者之间的一大区别是python使用0索引,而matlab使用1索引。你应该再看一遍这些东西。@JayanthKoushik轴零是什么意思?我知道Python使用索引零,而MatLab从一个坐标轴开始,0表示矩阵的第一维。所以你在这个维度上是出界的。