Advertisement
NikolayChukanov

Lab1

Dec 4th, 2023
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. import math
  2. import sympy as s
  3. import matplotlib.pyplot as plot
  4. import numpy as n
  5. from matplotlib.animation import FuncAnimation
  6.  
  7. t = s.Symbol('t')
  8.  
  9. x = s.sin(t)
  10. y = s.sin(2*t)
  11.  
  12. Vx = s.diff(x)
  13. Vy = s.diff(y)
  14.  
  15. step = 1000
  16. T = n.linspace(0, 10, step)
  17. X = n.zeros_like(T)
  18. Y = n.zeros_like(T)
  19. VX = n.zeros_like(T)
  20. VY = n.zeros_like(T)
  21. for i in n.arange(len(T)):
  22.     X[i] = s.Subs(x, t, T[i])
  23.     Y[i] = s.Subs(y, t, T[i])
  24.     VX[i] = s.Subs(Vx, t, T[i])
  25.     VY[i] = s.Subs(Vy, t, T[i])
  26.  
  27.  
  28. fgr = plot.figure()
  29. grf = fgr.add_subplot(1,1,1)
  30. grf.axis('equal')
  31. grf.set(xlim = [-2,2], ylim = [-2,2])
  32. grf.plot(X,Y)
  33.  
  34. Pnt = grf.plot(X[0], Y[0], marker = 'o')[0]
  35. Vpl = grf.plot([X[0], X[0]+VX[0]],[Y[0], Y[0]+VY[0]], 'r')[0]
  36.  
  37. def Vect_arrow(VecX, VecY, X, Y):
  38.     a = 0.3
  39.     b = 0.2
  40.     Arrx = n.array([-a, 0, -a])
  41.     Arry = n.array([b, 0, -b])
  42.  
  43.     phi = math.atan2(VecY, VecX)
  44.  
  45.     RotX = Arrx*n.cos(phi) - Arry*n.sin(phi)
  46.     RotY = Arrx*n.sin(phi) + Arry*n.cos(phi)
  47.  
  48.     Arrx = RotX + X+VecX
  49.     Arry = RotY + Y+VecY
  50.    
  51.     return Arrx, Arry
  52.  
  53. ArVX, ArVY = Vect_arrow(VX[0], VY[0], X[0], Y[0])
  54. Varr = grf.plot(ArVX, ArVY, 'red')[0]
  55.  
  56. def anim(i):
  57.     Pnt.set_data(X[i], Y[i])
  58.     Vpl.set_data([X[i], X[i]+VX[i]],[Y[i], Y[i]+VY[i]])
  59.     ArVX, ArVY = Vect_arrow(VX[i], VY[i], X[i], Y[i])
  60.     Varr.set_data(ArVX, ArVY)
  61.     return
  62.  
  63. an = FuncAnimation(fgr, anim, frames = step, interval = 1)
  64.  
  65. fgr.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement