对于循环索引问题

时间:2016-01-07 18:26:07

标签: 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(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

我觉得好像必须按照我使用np.zeros的方式做一些事情,但我以前做错了。我曾经看过人们在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

1 个答案:

答案 0 :(得分:2)

Jayanth Koushik是对的。

在Python中,第一个索引是0而不是1.您可能会发现这有用:

https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html