problem based upon coupled oscillatorcoupled oscillator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gazal sharma
    New Member
    • Aug 2014
    • 1

    problem based upon coupled oscillatorcoupled oscillator

    i have written program for coupled oscillator(nume rical solution)using euler method. but i am not getting the exact graph.can somebdy help me out of it.
    Code:
    K=2
    g=9.8
    L=8
    m=2.0
    Tm=10
    N=125
    h=Tm/float(N)
    import matplotlib.pyplot as plt
    import numpy as np
    import numpy as n1
    x1=n1.zeros(N+1)
    x2=np.zeros(N+1)
    v1=n1.zeros(N+1)
    v2=np.zeros(N+1)
    t=np.zeros(N+1)
    x1[0]=10
    x2[0]=0
    v1[0]=0
    v2[0]=0
    i=0
    while t[i]<Tm:
        v1[i+1]=(-g/L)*x1[i] - (K/m)*(x1[i]-x2[i])
        v2[i+1]=(-g/L)*x2[i] - (K/m)*(x2[i]-x1[i])
        x1[i+1]=x1[i] + v1[i+1]*h
        x2[i+1]=x2[i] + v2[i+1]*h
        t[i+1]=t[i]+h
        i=i+1
    plt.subplot(211)
    plt.plot(t,x1)
    plt.subplot(212)
    plt.plot(t,x2)
    plt.show()
    Last edited by bvdet; Aug 15 '14, 11:16 AM. Reason: Add code tags
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    but i am not getting the exact graph
    What does this mean. If you are using Python2.x then you should explicitly cast a divide operation to a float, otherwise it is possible to get an integer only as a result.
    Code:
    ## -g/float(L)
    v1[i+1]=(-g/float(L))*x1[i] - (K/m)*(x1[i]-x2[i]
    Also it is possible in the while t[i] statement for "i" to exceed the length of N+1 so you should test for that. Finally, do not use i,l, O, etc as single digit variable names as they look too much like numbers, and to make your code readable by others, take a look at the Python Style Guide, which suggests lower case with underlines for variable names.. An alternative to the while statement (logic only), although there is nothing wrong with the while statement
    Code:
    def check_t(t, Tm, N):
        for ctr in range(N+1):
            if t[ctr] >= Tm:
                return
            ## do the calcs 
    
        ##raise error as t[ctr] >= Tm was never hit

    Comment

    Working...