Yeah, what about it? The previous code have the both end bounded.
If we want a free/unbound end, we could set the condition at the with this properties (or we could choose whatever we like)
dy/dx=0
So we will have
y[1]-y[0]=0
y[0] = y[1]
if we want both free ends, we could set the other end as well
y[n] = y[n-1]
So, we just have to modify the original just a bit.
Beware though, with both ends free, we could lost the strings, :)
Here's the code
from pylab import *
import matplotlib.animation as animation
fig,ax = subplots()
def waves(y0,y1,cb):
y2 = y0
for i in range(1,len(y0)-1):
y2[i] = 2*y1[i]-y0[i]+cb*(y1[i+1]-2*y1[i]+y1[i-1])
y2[0] = y2[1]
#y2[len(y2)-1]=y2[len(y2)-2]
return y2
x = linspace(0.,1.,20)
dx = 1./(len(x))
y0 = sin(2*pi*x)
vy0 = 12.
b = 1./32. #dt2/dx2
dt = sqrt(b*dx*dx)
print dt
c = 1.
cb = c*b
y1 = y0 + vy0*dt
print y0
print y1
line, = ax.plot(x,y0)
def animate(i):
global y0,y1,cb
y2 = waves(y0,y1,cb)
y0 = y1
y1 = y2
line.set_ydata(y0)
return line,
#plot (x,y0)
ani = animation.FuncAnimation(fig, animate, np.arange(1,200), interval = 25, blit=False)
grid(True)
ylim(-10,10)
show()


No comments:
Post a Comment