2016-01-07 208 views
0

謝謝大家最近的幫助教我在Python中的錯誤。下面我發佈了一個Python代碼,假設它爲動態傳播創建動畫。For循環索引問題

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

@MorganThrapp剛添加它。 –

+5

我認爲主要的問題是,你直接將matlab代碼翻譯成python。但是兩者之間的一個很大的區別是python使用0索引,而matlab使用1索引。你應該再次審視這些事情。 –

回答