[Tutor] Solve wave equation
Evert Rol
evert.rol at gmail.com
Thu Feb 23 19:12:09 CET 2012
> OK so I can solve the equation but now I am having trouble plotting the solution! I would like to produce a surface plot with colors defined by p and animate it. That is plot the value of p at all x and z, over time (t). My code to get p is below but I really have no idea how to plot this. Anyone know the best way to go about this?
A quick search on "matplotlib surface plot" (since you're already using matplotlib) led me to http://matplotlib.sourceforge.net/mpl_toolkits/mplot3d/tutorial.html#surface-plots
Perhaps that's something you can use?
Your statement 'colors defined by p' leads more to using contour plots instead, and variants thereof: http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.contourf
I assume with "animate it", you just want to show every plot, continuously updating it. With matplotlib, that may be a bit slow, but you could save each figure to disk and then use some tool to put them into a movie (mpeg, wmv, animated gif, anything) if this is something you want to present to other people.
Evert
> thanks,
> D
>
> -------- Original Message --------
> Subject: Re: [Tutor] Solve wave equation
> Date: Thu, 23 Feb 2012 15:24:45 +0000
> From: David Craig <dcdavemail at gmail.com>
> To: tutor at python.org
>
> Hooray it works,
> thanks
>
> On 02/23/2012 01:39 PM, Ken Oliver wrote:
> > Do you really want dt = 1**-4 or would 10**-4 be better
> >
> > -----Original Message-----
> >> From: David Craig
> <dcdavemail at gmail.com>
>
> >> Sent: Feb 23, 2012 7:57 AM
> >> To:
> tutor at python.org
>
> >> Subject: [Tutor] Solve wave equation
> >>
> >> Hi,
> >> I am trying to write some code that will solve the 2D wave equation by
> >> the finite difference method, but it just returns an array full of zeros
> >> and NaN's. Not sure where I am going wrong, the code is attached so if
> >> could someone point me in the right direction I'd appreciate this.
> >> Thanks
> >> D
> >
> > .
> >
>
> # 2D Finite Distance Wave Equation.
> from pylab import *
> from numpy import math
>
> ion()
>
> # Set up variables.
> nx = 100
> nz = 100
> nsteps = 300
> c = 3500
> dt = 10**-4
> h = 1
> t = arange(0,nsteps,dt)
>
>
>
> # Define source as a spike.
> s = zeros(nsteps)
> s[1] = 1
> s[2] = 2
> s[3] = 1
>
> # Position source.
> xs = 50
> zs = 50
> ##plot(t,s)
> ##show()
>
> # Set up pressure field.
> p=empty([nx,nz,nsteps])
>
> for t in range(0,nsteps-1):
>
> for z in range(0,nz-1):
>
> for x in range(0,nx-1):
>
> p[x,z,t] = 0
>
>
> # Solve wave equation
>
> for t in range(2,nsteps-1):
>
> for z in range(1,nz-1):
>
> for x in range(2,nx-1):
>
> p[xs,zs,t] = s[t]
> k = (c*dt/h)**2
>
> p[x,z,t] = 2*p[x,z,t-1] - p[x,z,t-2] + k*(p[x+1,z,t-1]-4*p[x,z,t-1]+p[x-1,z,t-1]+p[x,z+1,t-1]+p[x,z-1,t-1])
>
>
> #Plot somehow
>
> draw()
>
> #close()
>
>
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list