|
From: surfcast23 <sur...@gm...> - 2012-07-01 16:50:54
|
Hi,
I am translating a Matlab code to python and get the following error when
the codes reaches the plotting section
Warning (from warnings module):
File "C:\Documents and Settings\My Documents\PHYSICS\Wave-eqn.py", line 40
w = (D*v)
RuntimeWarning: overflow encountered in multiply
Traceback (most recent call last):
File "C:\Documents and Settings\My Documents\PHYSICS\Wave-eqn\.py", line
50, in <module>
ax.plot_wireframe(x,tdata,data, rstride=10, cstride=10)
File "C:\Python32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line
906, in plot_wireframe
tylines = [tY[i] for i in cii]
File "C:\Python32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line
906, in <listcomp>
tylines = [tY[i] for i in cii]
IndexError: index out of bounds
My code
import numpy as np
from numpy import *
from math import pi
from scipy.linalg import toeplitz
from scipy.special import cotdg
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
N = 512
h = 2*np.pi/N
x = h*(np.arange(N) + 1)
t = 0
dt = h / 4
a = .1
tmax = 15;
tplot = .15;
nplots = int(round((tmax/tplot)));
plotgap = int(around(tplot/dt));
c = a + np.sin(x - 1)**2
v = np.exp(-100 * (x - 1)**2)
vold = np.exp(-100 * (x - a*dt - 1)**2)
#i = np.arange(1, N)
#column = np.hstack([0, .5 * (-1**i) * cotdg(i * h/2)])
#D = toeplitz(column, -column)
column = ((0.5*(-1)**arange(1,N+1))*cotdg(arange(1,N+1))*(h/2));
D = toeplitz(column,-column);print(D.shape);
k = np.zeros(((nplots,N))); print(v.shape);print(k.shape);
data = np.concatenate((v.reshape((512,1)).transpose(), k))#data =
np.concatenate((v, k),axis = 1);
#data = np.vstack([v,k]);
tdata = t;
for i in range(1,nplots+1):
for n in range(1,plotgap+1):
t = t+dt
w = (D*v)
vnew = vold-2*dt*c*w
vold = v
v = vnew
data[i,:] = v[0,:]
tdata = vstack([tdata, t])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
#X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(x,tdata,data, rstride=10, cstride=10)
plt.show()
I looked at the error line and it seems as if the y axes is where the
problem is, but I am not seeing why and would appreciate any help. Thank
you!
--
View this message in context: http://old.nabble.com/IndexError%3A-index-out-of-bounds-tp34098663p34098663.html
Sent from the matplotlib - users mailing list archive at Nabble.com.
|
|
From: Benjamin R. <ben...@ou...> - 2012-07-02 21:06:49
|
On Sun, Jul 1, 2012 at 12:50 PM, surfcast23 <sur...@gm...> wrote: > > Hi, > I am translating a Matlab code to python and get the following error when > the codes reaches the plotting section > > Warning (from warnings module): > File "C:\Documents and Settings\My Documents\PHYSICS\Wave-eqn.py", line > 40 > w = (D*v) > RuntimeWarning: overflow encountered in multiply > Traceback (most recent call last): > File "C:\Documents and Settings\My Documents\PHYSICS\Wave-eqn\.py", line > 50, in <module> > ax.plot_wireframe(x,tdata,data, rstride=10, cstride=10) > File "C:\Python32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line > 906, in plot_wireframe > tylines = [tY[i] for i in cii] > File "C:\Python32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line > 906, in <listcomp> > tylines = [tY[i] for i in cii] > IndexError: index out of bounds > > My code > > import numpy as np > from numpy import * > from math import pi > from scipy.linalg import toeplitz > from scipy.special import cotdg > from mpl_toolkits.mplot3d import axes3d > import matplotlib.pyplot as plt > > > > N = 512 > h = 2*np.pi/N > x = h*(np.arange(N) + 1) > t = 0 > dt = h / 4 > a = .1 > tmax = 15; > tplot = .15; > nplots = int(round((tmax/tplot))); > plotgap = int(around(tplot/dt)); > c = a + np.sin(x - 1)**2 > v = np.exp(-100 * (x - 1)**2) > vold = np.exp(-100 * (x - a*dt - 1)**2) > > #i = np.arange(1, N) > #column = np.hstack([0, .5 * (-1**i) * cotdg(i * h/2)]) > #D = toeplitz(column, -column) > > column = ((0.5*(-1)**arange(1,N+1))*cotdg(arange(1,N+1))*(h/2)); > D = toeplitz(column,-column);print(D.shape); > > k = np.zeros(((nplots,N))); print(v.shape);print(k.shape); > data = np.concatenate((v.reshape((512,1)).transpose(), k))#data = > np.concatenate((v, k),axis = 1); > #data = np.vstack([v,k]); > tdata = t; > > for i in range(1,nplots+1): > for n in range(1,plotgap+1): > t = t+dt > w = (D*v) > vnew = vold-2*dt*c*w > vold = v > v = vnew > data[i,:] = v[0,:] > tdata = vstack([tdata, t]) > > fig = plt.figure() > ax = fig.add_subplot(111, projection='3d') > #X, Y, Z = axes3d.get_test_data(0.05) > ax.plot_wireframe(x,tdata,data, rstride=10, cstride=10) > > plt.show() > > I looked at the error line and it seems as if the y axes is where the > problem is, but I am not seeing why and would appreciate any help. Thank > you! > numpy arrays are indexed starting at 0, not 1. So when you populate your "data" array with "data[i,:] = v[0,:]", and "i" only goes from 1 to nplots, data[0,:] is left completely uninitialized (unless it is being done by some of your pre-for-loop code, which is confusing to understand.) What I can tell you is that the error isn't in plot_wireframe() as much as the error exist with the inputs to plot_wireframe(). Perhaps the shapes aren't right or something. I will try and look at your code closer tomorrow and see if I can figure it out, but I suggest double-checking those arrays. Cheers! Ben Root |
|
From: surfcast23 <sur...@gm...> - 2012-07-31 22:07:03
|
I also got the dimensions of the arrays and was wandering if the problem might be there shape data = (101, 512) shape v = (512, 512) shape tdata = (101, 1) shape x = (512,) Benjamin Root-2 wrote: > > On Sun, Jul 1, 2012 at 12:50 PM, surfcast23 <sur...@gm...> wrote: > >> >> Hi, >> I am translating a Matlab code to python and get the following error >> when >> the codes reaches the plotting section >> >> Warning (from warnings module): >> File "C:\Documents and Settings\My Documents\PHYSICS\Wave-eqn.py", line >> 40 >> w = (D*v) >> RuntimeWarning: overflow encountered in multiply >> Traceback (most recent call last): >> File "C:\Documents and Settings\My Documents\PHYSICS\Wave-eqn\.py", >> line >> 50, in <module> >> ax.plot_wireframe(x,tdata,data, rstride=10, cstride=10) >> File "C:\Python32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", >> line >> 906, in plot_wireframe >> tylines = [tY[i] for i in cii] >> File "C:\Python32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", >> line >> 906, in <listcomp> >> tylines = [tY[i] for i in cii] >> IndexError: index out of bounds >> >> My code >> >> import numpy as np >> from numpy import * >> from math import pi >> from scipy.linalg import toeplitz >> from scipy.special import cotdg >> from mpl_toolkits.mplot3d import axes3d >> import matplotlib.pyplot as plt >> >> >> >> N = 512 >> h = 2*np.pi/N >> x = h*(np.arange(N) + 1) >> t = 0 >> dt = h / 4 >> a = .1 >> tmax = 15; >> tplot = .15; >> nplots = int(round((tmax/tplot))); >> plotgap = int(around(tplot/dt)); >> c = a + np.sin(x - 1)**2 >> v = np.exp(-100 * (x - 1)**2) >> vold = np.exp(-100 * (x - a*dt - 1)**2) >> >> #i = np.arange(1, N) >> #column = np.hstack([0, .5 * (-1**i) * cotdg(i * h/2)]) >> #D = toeplitz(column, -column) >> >> column = ((0.5*(-1)**arange(1,N+1))*cotdg(arange(1,N+1))*(h/2)); >> D = toeplitz(column,-column);print(D.shape); >> >> k = np.zeros(((nplots,N))); print(v.shape);print(k.shape); >> data = np.concatenate((v.reshape((512,1)).transpose(), k))#data = >> np.concatenate((v, k),axis = 1); >> #data = np.vstack([v,k]); >> tdata = t; >> >> for i in range(1,nplots+1): >> for n in range(1,plotgap+1): >> t = t+dt >> w = (D*v) >> vnew = vold-2*dt*c*w >> vold = v >> v = vnew >> data[i,:] = v[0,:] >> tdata = vstack([tdata, t]) >> >> fig = plt.figure() >> ax = fig.add_subplot(111, projection='3d') >> #X, Y, Z = axes3d.get_test_data(0.05) >> ax.plot_wireframe(x,tdata,data, rstride=10, cstride=10) >> >> plt.show() >> >> I looked at the error line and it seems as if the y axes is where the >> problem is, but I am not seeing why and would appreciate any help. Thank >> you! >> > > numpy arrays are indexed starting at 0, not 1. So when you populate your > "data" array with "data[i,:] = v[0,:]", and "i" only goes from 1 to > nplots, > data[0,:] is left completely uninitialized (unless it is being done by > some > of your pre-for-loop code, which is confusing to understand.) > > What I can tell you is that the error isn't in plot_wireframe() as much as > the error exist with the inputs to plot_wireframe(). Perhaps the shapes > aren't right or something. I will try and look at your code closer > tomorrow and see if I can figure it out, but I suggest double-checking > those arrays. > > Cheers! > Ben Root > > ------------------------------------------------------------------------------ > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > -- View this message in context: http://old.nabble.com/IndexError%3A-index-out-of-bounds-tp34098663p34238189.html Sent from the matplotlib - users mailing list archive at Nabble.com. |
|
From: surfcast23 <sur...@gm...> - 2012-07-03 18:01:18
|
I will try initializing starting at 0 Benjamin Root-2 wrote: > > On Sun, Jul 1, 2012 at 12:50 PM, surfcast23 <sur...@gm...> wrote: > >> >> Hi, >> I am translating a Matlab code to python and get the following error >> when >> the codes reaches the plotting section >> >> Warning (from warnings module): >> File "C:\Documents and Settings\My Documents\PHYSICS\Wave-eqn.py", line >> 40 >> w = (D*v) >> RuntimeWarning: overflow encountered in multiply >> Traceback (most recent call last): >> File "C:\Documents and Settings\My Documents\PHYSICS\Wave-eqn\.py", >> line >> 50, in <module> >> ax.plot_wireframe(x,tdata,data, rstride=10, cstride=10) >> File "C:\Python32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", >> line >> 906, in plot_wireframe >> tylines = [tY[i] for i in cii] >> File "C:\Python32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", >> line >> 906, in <listcomp> >> tylines = [tY[i] for i in cii] >> IndexError: index out of bounds >> >> My code >> >> import numpy as np >> from numpy import * >> from math import pi >> from scipy.linalg import toeplitz >> from scipy.special import cotdg >> from mpl_toolkits.mplot3d import axes3d >> import matplotlib.pyplot as plt >> >> >> >> N = 512 >> h = 2*np.pi/N >> x = h*(np.arange(N) + 1) >> t = 0 >> dt = h / 4 >> a = .1 >> tmax = 15; >> tplot = .15; >> nplots = int(round((tmax/tplot))); >> plotgap = int(around(tplot/dt)); >> c = a + np.sin(x - 1)**2 >> v = np.exp(-100 * (x - 1)**2) >> vold = np.exp(-100 * (x - a*dt - 1)**2) >> >> #i = np.arange(1, N) >> #column = np.hstack([0, .5 * (-1**i) * cotdg(i * h/2)]) >> #D = toeplitz(column, -column) >> >> column = ((0.5*(-1)**arange(1,N+1))*cotdg(arange(1,N+1))*(h/2)); >> D = toeplitz(column,-column);print(D.shape); >> >> k = np.zeros(((nplots,N))); print(v.shape);print(k.shape); >> data = np.concatenate((v.reshape((512,1)).transpose(), k))#data = >> np.concatenate((v, k),axis = 1); >> #data = np.vstack([v,k]); >> tdata = t; >> >> for i in range(1,nplots+1): >> for n in range(1,plotgap+1): >> t = t+dt >> w = (D*v) >> vnew = vold-2*dt*c*w >> vold = v >> v = vnew >> data[i,:] = v[0,:] >> tdata = vstack([tdata, t]) >> >> fig = plt.figure() >> ax = fig.add_subplot(111, projection='3d') >> #X, Y, Z = axes3d.get_test_data(0.05) >> ax.plot_wireframe(x,tdata,data, rstride=10, cstride=10) >> >> plt.show() >> >> I looked at the error line and it seems as if the y axes is where the >> problem is, but I am not seeing why and would appreciate any help. Thank >> you! >> > > numpy arrays are indexed starting at 0, not 1. So when you populate your > "data" array with "data[i,:] = v[0,:]", and "i" only goes from 1 to > nplots, > data[0,:] is left completely uninitialized (unless it is being done by > some > of your pre-for-loop code, which is confusing to understand.) > > What I can tell you is that the error isn't in plot_wireframe() as much as > the error exist with the inputs to plot_wireframe(). Perhaps the shapes > aren't right or something. I will try and look at your code closer > tomorrow and see if I can figure it out, but I suggest double-checking > those arrays. > > Cheers! > Ben Root > > ------------------------------------------------------------------------------ > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > -- View this message in context: http://old.nabble.com/IndexError%3A-index-out-of-bounds-tp34098663p34109116.html Sent from the matplotlib - users mailing list archive at Nabble.com. |
|
From: surfcast23 <sur...@gm...> - 2012-07-31 20:43:40
|
Hi Ben,
I was working on some other things and have finally gotten a chance to get
back to this. I changed the for loops to
for i in range(nplots):
for n in range(plotgap):
t = t+dt
w = (D*v)
vnew = vold-2*dt*c*w
vold = v
v = vnew
data[i,:] = v[0,:]
tdata = vstack([tdata, t])
But I still get the same error as in my original post.
Khary
surfcast23 wrote:
>
> I will try initializing starting at 0
>
> Benjamin Root-2 wrote:
>>
>> On Sun, Jul 1, 2012 at 12:50 PM, surfcast23 <sur...@gm...> wrote:
>>
>>>
>>> Hi,
>>> I am translating a Matlab code to python and get the following error
>>> when
>>> the codes reaches the plotting section
>>>
>>> Warning (from warnings module):
>>> File "C:\Documents and Settings\My Documents\PHYSICS\Wave-eqn.py",
>>> line
>>> 40
>>> w = (D*v)
>>> RuntimeWarning: overflow encountered in multiply
>>> Traceback (most recent call last):
>>> File "C:\Documents and Settings\My Documents\PHYSICS\Wave-eqn\.py",
>>> line
>>> 50, in <module>
>>> ax.plot_wireframe(x,tdata,data, rstride=10, cstride=10)
>>> File "C:\Python32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py",
>>> line
>>> 906, in plot_wireframe
>>> tylines = [tY[i] for i in cii]
>>> File "C:\Python32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py",
>>> line
>>> 906, in <listcomp>
>>> tylines = [tY[i] for i in cii]
>>> IndexError: index out of bounds
>>>
>>> My code
>>>
>>> import numpy as np
>>> from numpy import *
>>> from math import pi
>>> from scipy.linalg import toeplitz
>>> from scipy.special import cotdg
>>> from mpl_toolkits.mplot3d import axes3d
>>> import matplotlib.pyplot as plt
>>>
>>>
>>>
>>> N = 512
>>> h = 2*np.pi/N
>>> x = h*(np.arange(N) + 1)
>>> t = 0
>>> dt = h / 4
>>> a = .1
>>> tmax = 15;
>>> tplot = .15;
>>> nplots = int(round((tmax/tplot)));
>>> plotgap = int(around(tplot/dt));
>>> c = a + np.sin(x - 1)**2
>>> v = np.exp(-100 * (x - 1)**2)
>>> vold = np.exp(-100 * (x - a*dt - 1)**2)
>>>
>>> #i = np.arange(1, N)
>>> #column = np.hstack([0, .5 * (-1**i) * cotdg(i * h/2)])
>>> #D = toeplitz(column, -column)
>>>
>>> column = ((0.5*(-1)**arange(1,N+1))*cotdg(arange(1,N+1))*(h/2));
>>> D = toeplitz(column,-column);print(D.shape);
>>>
>>> k = np.zeros(((nplots,N))); print(v.shape);print(k.shape);
>>> data = np.concatenate((v.reshape((512,1)).transpose(), k))#data =
>>> np.concatenate((v, k),axis = 1);
>>> #data = np.vstack([v,k]);
>>> tdata = t;
>>>
>>> for i in range(1,nplots+1):
>>> for n in range(1,plotgap+1):
>>> t = t+dt
>>> w = (D*v)
>>> vnew = vold-2*dt*c*w
>>> vold = v
>>> v = vnew
>>> data[i,:] = v[0,:]
>>> tdata = vstack([tdata, t])
>>>
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111, projection='3d')
>>> #X, Y, Z = axes3d.get_test_data(0.05)
>>> ax.plot_wireframe(x,tdata,data, rstride=10, cstride=10)
>>>
>>> plt.show()
>>>
>>> I looked at the error line and it seems as if the y axes is where the
>>> problem is, but I am not seeing why and would appreciate any help. Thank
>>> you!
>>>
>>
>> numpy arrays are indexed starting at 0, not 1. So when you populate your
>> "data" array with "data[i,:] = v[0,:]", and "i" only goes from 1 to
>> nplots,
>> data[0,:] is left completely uninitialized (unless it is being done by
>> some
>> of your pre-for-loop code, which is confusing to understand.)
>>
>> What I can tell you is that the error isn't in plot_wireframe() as much
>> as
>> the error exist with the inputs to plot_wireframe(). Perhaps the shapes
>> aren't right or something. I will try and look at your code closer
>> tomorrow and see if I can figure it out, but I suggest double-checking
>> those arrays.
>>
>> Cheers!
>> Ben Root
>>
>> ------------------------------------------------------------------------------
>> Live Security Virtual Conference
>> Exclusive live event will cover all the ways today's security and
>> threat landscape has changed and how IT managers can respond. Discussions
>> will include endpoint security, mobile security and the latest in malware
>> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
>> _______________________________________________
>> Matplotlib-users mailing list
>> Mat...@li...
>> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>>
>>
>
>
--
View this message in context: http://old.nabble.com/IndexError%3A-index-out-of-bounds-tp34098663p34237855.html
Sent from the matplotlib - users mailing list archive at Nabble.com.
|