Reputation: 1354
thank you all for you recent help teaching me my wrongs in Python. Below I posted a Python code that is suppose to animate a wave propagation.
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)
Using this code I get an error saying
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
I feel as if it has to do something with the way I am using np.zeros, but I have been wrong before. I have seen people post about this error before on stackexchange but I have been able to figure it out, I was thinking that the issue is caused by dimensional issues between arrays.
Below is the MatLab code I have been using for a reference.
%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
Upvotes: 0
Views: 74
Reputation: 73
Jayanth Koushik is correct.
In Python, the first index is 0, not 1. You may find this helpful:
https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html
Upvotes: 2