Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from mpl_toolkits.mplot3d import Axes3D
  4. from random import randint
  5. from matplotlib import animation
  6.  
  7. #x - prey
  8. #y - predator
  9.  
  10. def get_chart(x0, y0, time, h, alpha, beta, gamma, delta):
  11. all_x = []
  12. all_y = []
  13. x_prior = x0
  14. y_prior = y0
  15. for i in range(0, len(time)):
  16. x = x_prior + h*(alpha*x_prior - beta*x_prior*y_prior)
  17. y = y_prior + h*(delta*x*y_prior - gamma*y_prior)
  18. all_x.append(x_prior)
  19. all_y.append(y_prior)
  20. x_prior = x
  21. y_prior = y
  22. return all_x, all_y
  23.  
  24. def printPoint(x,y):
  25. point = '(' + str(x) + ',' + str(y) + ')'
  26. return point
  27.  
  28. def rand_colour_set(charts):
  29. colour = []
  30. for i in range(charts):
  31. colour.append(np.random.random(3))
  32. return colour
  33.  
  34. def rand_chart_set(charts, h, alpha, beta, gamma, delta, time, min, max):
  35. X_set = []
  36. Y_set = []
  37. for i in range(charts):
  38. prey = randint(min, max)
  39. predator = randint(min, max)
  40. line1, line2 = get_chart(prey, predator, time, h, alpha, beta, gamma, delta)
  41. X_set.append(line1)
  42. Y_set.append(line2)
  43. return (X_set, Y_set)
  44.  
  45. def chart_3D(X_set, Y_set, time, ax3D, colour_set):
  46. for i in range(0, len(X_set)):
  47. line1 = X_set[i]
  48. line2 = Y_set[i]
  49. ch = str(i + 1) + '(ofiara, drapieżnik): ' + printPoint(line1[0], line2[0])
  50. color = colour_set[i]
  51. ax3D.plot(line1, line2, time, color = color, label = ch)
  52. if(i == 0):
  53. ax3D.scatter(line1[0], line2[0], time[0], c='g', label='start')
  54. ax3D.scatter(line1[len(line1) - 1], line2[len(line1) - 1], time[len(line1) - 1], c='r', label='end')
  55. else:
  56. ax3D.scatter(line1[0], line2[0], time[0], c='g')
  57. ax3D.scatter(line1[len(line1) - 1], line2[len(line1) - 1], time[len(line1) - 1], c='r')
  58.  
  59. T, t0 = 50, 0
  60. h = 0.01
  61. alpha, beta, gamma, delta = 2, 1.1 , 1 ,0.9
  62. min, max = 1, 30
  63. charts = 7
  64. time = np.arange(t0, T, h).tolist()
  65. X_set, Y_set = rand_chart_set(charts, h, alpha, beta, gamma, delta, time, min, max)
  66.  
  67. fig2D = plt.figure()
  68. ax2D = fig2D.add_subplot(111)
  69. colours = rand_colour_set(charts)
  70. charts_animate = [ax2D.plot([],[], c = colours[i], label = str(i + 1))[0] for i in range(charts)]
  71.  
  72.  
  73. def animate(frame):
  74. for i,ch in enumerate(charts_animate):
  75. ch.set_data(X_set[i][:frame], Y_set[i][:frame])
  76. return charts_animate
  77.  
  78. anim = animation.FuncAnimation(fig2D, animate, interval=0.01, blit=True)
  79.  
  80. ax2D.set_xlabel('ofiary')
  81. ax2D.set_ylabel('drapieżnicy')
  82. ax2D.set_title("model ofirara-drapieżnik 2D")
  83. ax2D.axis([-5,max + 20,-5,max + 10])
  84.  
  85. fig3D = plt.figure()
  86. ax3D = fig3D.add_subplot(111, projection = '3d')
  87. ax3D.set_xlabel("ofiary")
  88. ax3D.set_ylabel("drapieżnicy")
  89. ax3D.set_zlabel("czas")
  90. ax3D.set_title("model ofirara-drapieżnik 3D")
  91.  
  92. chart_3D(X_set, Y_set, time, ax3D, colours)
  93. ax2D.legend(loc = 'best')
  94. ax3D.legend(loc = 'best')
  95.  
  96. ax2D.grid()
  97. ax3D.grid()
  98.  
  99. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement