Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 KB | None | 0 0
  1. from sympy import *
  2. import sympy as sy
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import matplotlib.animation as animation
  6. import math
  7.  
  8. def drawLine(pa, pb):
  9.     lx = np.zeros(2)
  10.     ly = np.zeros(2)
  11.  
  12.     lx[0] = -100
  13.     lx[1] = 100
  14.  
  15.     ly[0] = pa*lx[0] + pb
  16.     ly[1] = pa*lx[1] + pb
  17.  
  18.     return lx, ly
  19.  
  20. def drawCircle(sx, sy):
  21.     cx = np.zeros(100)
  22.     cy = np.zeros(100)
  23.     step = (2*math.pi)/100
  24.     for i in range(0, 100):
  25.         cx[i] = sx+math.sin(i*step)
  26.         cy[i] = sy+math.cos(i*step)
  27.  
  28.     return cx, cy
  29.  
  30.  
  31. def quadraticcase2(t0, tend, dt, k, fun1, fun2, teta, x0):  # solver for the case of quadratic energy
  32.     # fun is a linear function with time in argument; dt is the change in time
  33.     f1 = sy.sympify(fun1)  # converting string into function with t in argument
  34.     f2 = sy.sympify(fun2)
  35.     t = np.arange(t0, tend, dt)  # time grid\
  36.     n = len(t)
  37.     x = np.zeros((n, 2))  # x-grid
  38.     e = np.zeros((n, 2))
  39.     a = np.zeros(n)
  40.     b = np.zeros(n)
  41.  
  42.     ki = k #np.linalg.inv(k)
  43.  
  44.     x[0] = x0
  45.     e[0][0] = (f1.subs("t", t[0]) - teta) * ki[0][0] + (f1.subs("t", t[0]) + teta) * ki[1][0]
  46.     e[0][1] = (f2.subs("t", t[0]) - teta) * ki[0][1] + (f2.subs("t", t[0]) + teta) * ki[1][1]
  47.     print(e[0][0], ",", e[0][1])
  48.     x[0] = e[0]
  49.  
  50.     for i in range(1, n):
  51.         e[i][0] = (f1.subs("t", t[i]) - teta) * ki[0][0] + (f1.subs("t", t[i]) + teta) * ki[1][0]
  52.         e[i][1] = (f2.subs("t", t[i]) - teta) * ki[0][1] + (f2.subs("t", t[i]) + teta) * ki[1][1]
  53.  
  54.         dist = np.linalg.norm(e[i]-x[i-1])
  55.  
  56.         if dist <= 1:
  57.             x[i] = x[i - 1]
  58.         else:
  59.             Vx = e[i][0]-e[i-1][0]
  60.             Vy = e[i][1]-e[i-1][1]
  61.  
  62.  
  63.             a[i] = Vy/Vx
  64.             b[i] = x[i-1][1] - a[i]*x[i-1][0]
  65.  
  66.             A = a[i]+1
  67.             B = 0 - 2*e[i][0] + 2*a[i]*(b[i]-e[i][1])
  68.             C = math.pow(e[i][0], 2) - math.pow((b[i]-e[i][1]), 2) - 1
  69.  
  70.             delta = B*B - 4*A*C
  71.             sd = math.sqrt(delta)
  72.  
  73.             p1x = (0 - B - sd) / (2*A)
  74.             p2x = (0 - B + sd) / (2*A)
  75.  
  76.             p1y = a[i]*p1x + b[i]
  77.             p2y = a[i]*p2x + b[i]
  78.  
  79.             print(e[i][0], e[i][1], A, B, C, delta, "; p1 ", p1x, ",", p1y, " p2 ", p2x, ",", p2y)
  80.  
  81.             distp1 = math.sqrt(math.pow(x[i - 1][0] - p1x, 2) + math.pow(x[i - 1][1] - p1y, 2))
  82.             distp2 = math.sqrt(math.pow(x[i - 1][0] - p2x, 2) + math.pow(x[i - 1][1] - p2y, 2))
  83.  
  84.             if distp1 < distp2:
  85.                 x[i][0] = p1x
  86.                 x[i][1] = p1y
  87.             else:
  88.                 x[i][0] = p2x
  89.                 x[i][1] = p2y
  90.  
  91.     fig = plt.figure()
  92.     ax = plt.axes(xlim=(-4, 4), ylim=(-4, 4))
  93.     line1, = ax.plot([], [])
  94.     line2, = ax.plot([], [], ':')
  95.     line3, = ax.plot([], [], ':')
  96.     line4, = ax.plot([], [])
  97.  
  98.     xdata = []
  99.     ydata = []
  100.     exdata = []
  101.     eydata = []
  102.  
  103.     def init():  # only required for blitting to give a clean slate.
  104.         line1.set_data([], [])
  105.         line2.set_data([], [])
  106.         line3.set_data([], [])
  107.         line4.set_data([], [])
  108.         xdata.clear()
  109.         ydata.clear()
  110.         exdata.clear()
  111.         eydata.clear()
  112.         return line1, line2, line3,
  113.  
  114.     def animate(i):
  115.         xdata.append(x[i][0])
  116.         ydata.append(x[i][1])
  117.         exdata.append(e[i][0])
  118.         eydata.append(e[i][1])
  119.         cx, cy = drawCircle(e[i][0], e[i][1])
  120.         lx, ly = drawLine(a[i], b[i])
  121.         line1.set_data(cx, cy)
  122.         line2.set_data(xdata, ydata)
  123.         line3.set_data(exdata, eydata)
  124.         line4.set_data(lx, ly)
  125.         return line1, line2, line3, line4
  126.  
  127.     ani = animation.FuncAnimation(fig, animate, init_func=init, interval=200, blit=True, save_count=1, frames=100)
  128.     ani.save("movie.gif")
  129.  
  130.     plt.show()
  131.  
  132.     return x, t
  133.  
  134.  
  135. quadraticcase2(t0=0.0, tend=1.6, dt=0.1, k=np.array([[1, 0], [0, 1]]), fun1="1.5*sin(t)", fun2="1.5*cos(t)", teta=0, x0=np.array([0.5, 0.0]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement