【问题标题】:For loop indexing issueFor循环索引问题
【发布时间】:2016-01-07 18:26:06
【问题描述】:

感谢你们最近帮助我教了我在 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

【问题讨论】:

  • 请将完整的回溯添加到您的帖子中。它包括行号等内容。
  • @MorganThrapp 刚刚添加了它。
  • 我认为主要问题是你直接将matlab代码翻译成python。但两者之间的一大区别是 python 使用 0 索引,而 matlab 使用 1 索引。你应该重新审视这些事情。
  • @JayanthKoushik 零轴是什么意思?我知道 Python 使用索引零,而 MatLab 将从一开始
  • Axis 0 是指矩阵的第一个维度。所以你在那个维度上超出了界限。

标签: python matlab indexing


【解决方案1】:

Jayanth Koushik 是正确的。

在 Python 中,第一个索引是 0,而不是 1。您可能会发现这很有帮助:

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

【讨论】:

    猜你喜欢
    • 2015-01-19
    • 2015-02-14
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 2021-12-20
    • 1970-01-01
    • 2021-04-07
    相关资源
    最近更新 更多