Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python Wave on string: How can I create a traveling triangle pulse?

  1. May 8, 2017 #1
    I have the following program that moves a wave on a string with fixed ends. The program solves the wave equation given a initial condition wave. The initial condition is a triangle wave splitting into two pulses.

    Here is the code written in Python:

    Code (Python):

    from numpy import *
    from  matplotlib.pyplot import *

    N = 201 # Number of mass points
    nT = 1200 # Number of time points
    mi = 0.02
    m = [mi]*(N) # mass array of points on string
    m[-1] = 0.02
    m[0] = 0.02
    ki = 10.
    k = [ki]*(N) # Spring constant
    k[-1] = 0
    k[0] = 0
    dx = 0.295
    kappa = ki*dx
    my = mi/dx
    c = sqrt(kappa/my) # velocity
    print c
    dt = 0.9*dx/c # Courier

    x = arange( N )*dx
    t = arange( N )*dt

    y = zeros( [N, nT ] )

    #Initial condition with t= 0
    for i in range(N-1):
        if (70 <= i <= 99):
            y[i,0] = (i - 69.)/30
        elif (100 <= i <= 128):
            y[i,0] = ((129 - i)/30.)
            y[i,0] = 0.

    #Wave equation
    for j in range(nT-1):
        for i in range(N-1):
            if j == 0:
                y[i,j+1] = 2*y[i,j] - y[i,j] + (dt**2/m[i])*(k[i-1]*y[i+1,j] - 2*k[i-1]*y[i,j] + k[i]*y[i-1,j] )
                y[i,j+1] = 2*y[i,j] - y[i,j-1] + (dt**2/m[i])*( k[i-1]*y[i+1,j] - 2*k[i-1]*y[i,j] + k[i]*y[i-1,j] )


    Here is the plot:


    I want to make changes to my program so the one triangle wave pulse moves to the right and gets reflected instead of splitting up in two opposite moving pulses. How can I change my initial conditions to make this possible? I want something like this:
    Wave_equation_1D_fixed_endpoints (1).gif

    EDIT:I am not sure if the pictures was correctly added!
    Last edited: May 8, 2017
  2. jcsd
  3. May 8, 2017 #2


    Staff: Mentor

    Nope the pictures didn't make it, you need to try again.
  4. May 8, 2017 #3


    User Avatar

    Staff: Mentor

    The pictures have been Uploaded now. Thanks @jedishrfu :smile:
  5. May 8, 2017 #4
    You need to specify the initial speed of the triangle wave as well. Or you could move one of the ends. That's what you would do with a real string.
  6. May 9, 2017 #5
    But how can I implement this initial velocity?
  7. May 9, 2017 #6
    It's more usual, to use 2 arrays, one of them with the positions and one of the with the velocities, and then use y[x][t] += v[x][t] * dt and v[x][t] += dt * acceleration,
    What you have done is to store the complete history of the positions of the string. You then use the difference between the two last values of the position to add to the last postion to get the next position.
    your y[i,j+1] = 2*y[i,j] - y[i,j-1] + ...... does this. But this doesn't work if j is 0, so you use y[i,j+1] = y[i,j] + .... . The effect of this is to make the initial velocity 0.
    If you want to set a nonzero initial velocity you must initializie both y[i,0] and y[i,1] to different values
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted

Similar Discussions: Wave on string: How can I create a traveling triangle pulse?
  1. How can I code this? (Replies: 1)

  2. How i can learn ? (Replies: 16)