title: Python: Writing a program to simulate 1D wave motion along a string
body: <p>I am working on a program that simulates wave motion along a 1-dimensional string to eventually simulate different wave packets. I found a program in the book "Python Scripting for Computational Science" that claims to describe wave motion, though I'm not certain how to implement it (the book was on Google Books and won't show me the text before/after the code). </p>
  
  <p>For example, I understand that "f" is a function of x and t and that "I" is a function of x but what functions are actually needed to produce a wave?</p>
  
  <pre><code>I= 
  f= 
  c= 
  L= 
  n=
  dt=
  tstop=
  
  
  x = linespace(0,L,n+1) #grid points in x dir
  dx = L/float(n)
  if dt &lt;= 0: dt = dx/float(c) #max step time
  C2 = (c*dt/dx)**2  #help variable in the scheme
  dt2 = dt*dt
  
  up = zeros(n+1) #NumPy solution array
  u = up.copy()   #solution at t-dt
  um = up.copy()    #solution at t-2*dt
  
  t = 0.0
  for i in iseq(0,n):
  u[i] +0.5*C2*(u[i-1] - 2*u[i] +u[i+1]) + \
      dt2*f(x[i], t)
  um[0] = 0;   um[n] = 0
  
  while t&lt;= tstop:
  t_old = t; t+=dt
  #update all inner points:
  for i in iseq(start=1, stop= n-1):
  up[i] = -um[i] +2*u[i] + \
      C2*(u[i-1] - 2*u[i] + u[i+1]) + \
      dt2*f(x[i], t_old)
  
  #insert boundary conditions
  up[0] = 0; up[n] = 0
  #updata data structures for next step
  um = u.copy(); u = up.copy()
  </code></pre>