By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,349 Members | 1,263 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,349 IT Pros & Developers. It's quick & easy.

problem based upon coupled oscillatorcoupled oscillator

P: 1
i have written program for coupled oscillator(numerical solution)using euler method. but i am not getting the exact graph.can somebdy help me out of it.
Expand|Select|Wrap|Line Numbers
  1. K=2
  2. g=9.8
  3. L=8
  4. m=2.0
  5. Tm=10
  6. N=125
  7. h=Tm/float(N)
  8. import matplotlib.pyplot as plt
  9. import numpy as np
  10. import numpy as n1
  11. x1=n1.zeros(N+1)
  12. x2=np.zeros(N+1)
  13. v1=n1.zeros(N+1)
  14. v2=np.zeros(N+1)
  15. t=np.zeros(N+1)
  16. x1[0]=10
  17. x2[0]=0
  18. v1[0]=0
  19. v2[0]=0
  20. i=0
  21. while t[i]<Tm:
  22.     v1[i+1]=(-g/L)*x1[i] - (K/m)*(x1[i]-x2[i])
  23.     v2[i+1]=(-g/L)*x2[i] - (K/m)*(x2[i]-x1[i])
  24.     x1[i+1]=x1[i] + v1[i+1]*h
  25.     x2[i+1]=x2[i] + v2[i+1]*h
  26.     t[i+1]=t[i]+h
  27.     i=i+1
  28. plt.subplot(211)
  29. plt.plot(t,x1)
  30. plt.subplot(212)
  31. plt.plot(t,x2)
Aug 15 '14 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 626
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.
Expand|Select|Wrap|Line Numbers
  1. ## -g/float(L)
  2. 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
Expand|Select|Wrap|Line Numbers
  1. def check_t(t, Tm, N):
  2.     for ctr in range(N+1):
  3.         if t[ctr] >= Tm:
  4.             return
  5.         ## do the calcs 
  7.     ##raise error as t[ctr] >= Tm was never hit
Aug 15 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.