Advertisement
Guest User

Untitled

a guest
Apr 16th, 2024
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.09 KB | Source Code | 0 0
  1. from math import radians, cos, sin, asin, sqrt
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. plt.rcParams.update({'font.size': 14})
  5. plt.rc('text', usetex=True)
  6. plt.rc('text.latex', preamble=r'\usepackage{amsmath}')
  7. plt.rcParams["font.family"] = "Times New Roman"
  8.  
  9. ###############################################################################
  10. # Distance London to Sydney                                                   #
  11. ###############################################################################
  12.  
  13. # https://www.latlong.net/
  14. coord_london = (51.470020,-0.454295) # Heathrow Airport, London, UK
  15. coord_sydney = (-33.947346,151.179428) # Sydney Airport, NSW, Australia
  16.  
  17. def haversine(coord1, coord2):
  18.     """source: https://stackoverflow.com/a/4913653"""
  19.     lon1, lat1, lon2, lat2 = map(radians, [coord1[1], coord1[0], coord2[1], coord2[0]])
  20.     dlon = lon2 - lon1
  21.     dlat = lat2 - lat1
  22.     a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
  23.     c = 2 * asin(sqrt(a))
  24.     r = 6371 # Earth's radius (km)
  25.     return c * r
  26.  
  27. distance = haversine(coord_london, coord_sydney) # km
  28. print(f"Distance from London to Sydney: {distance:.2f} km")
  29.  
  30.  
  31. ###############################################################################
  32. # Travel time                                                                 #
  33. ###############################################################################
  34.  
  35. a = 1.23 # maximum comfortable m/s^2 (https://doi.org/10.1016/j.apergo.2022.103881, p.8)
  36. distance *= 1000 # convert distance to meters
  37. t = 0 # s
  38. v = 0 # m/s
  39. s = 0 # m
  40. data = [] # (velocity, distance) for plotting
  41.  
  42. # Acceleration
  43. while s < distance/2:
  44.     t += 1
  45.     v += a
  46.     s += v
  47.     data.append((v*3.6, s/1000))
  48.  
  49. print(f"Maximum velocity: {v*3.6:.2f} km/h at {t/3600:.2f} h ({t:.0f} s)")
  50.  
  51. # Deceleration
  52. while s < distance:
  53.     t += 1
  54.     v -= a
  55.     s += v
  56.     data.append((v*3.6, s/1000))
  57.  
  58. print(f"Travel time from London to Sydney: {t/3600:.2f} hours ({t:.0f} s)")
  59.  
  60.  
  61. ###############################################################################
  62. # Plotting                                                                    #
  63. ###############################################################################
  64.  
  65. fig, axs = plt.subplots(2, 2, figsize=(12, 6))
  66. fig.suptitle("Travel from London to Sydney")
  67.  
  68. # Plot velocity vs time
  69. axs[0,0].plot(np.arange(0, t, 1), list(zip(*data))[0], 'b-')
  70. axs[0,0].set_xlabel('Time [s]')
  71. axs[0,0].set_ylabel('Velocity [km/h]')
  72. axs[0,0].set_ylim(top=17500)
  73. axs[0,0].set_yticks(np.arange(0, 17501, 2500))
  74. axs[0,0].set_xlim(right=8000)
  75. axs[0,0].set_xticks(np.arange(0, 8001, 1000))
  76. axs[0,0].grid(True, axis='both', which='both', linestyle='--', linewidth=0.5)
  77.  
  78. # Plot distance vs velocity
  79. axs[0,1].plot(list(zip(*data))[1], list(zip(*data))[0], 'r-')
  80. axs[0,1].set_xlabel('Distance [km]')
  81. axs[0,1].set_ylabel('Velocity [km/h]')
  82. axs[0,1].set_ylim(top=17500)
  83. axs[0,1].set_yticks(np.arange(0, 17501, 2500))
  84. axs[0,1].set_xlim(right=17500)
  85. axs[0,1].set_xticks(np.arange(0, 17501, 2500))
  86. axs[0,1].grid(True, axis='both', which='both', linestyle='--', linewidth=0.5)
  87.  
  88. # Plot acceleration vs time
  89. axs[1,0].plot(np.arange(0, round(t/2), 1), np.full(round(t/2), a), 'g-')
  90. axs[1,0].plot(np.arange(round(t/2), t, 1), np.full(round(t/2)+1, -a), 'g-')
  91. axs[1,0].set_xlabel('Time [s]')
  92. axs[1,0].set_ylabel('Acceleration [m/s$^2$]')
  93. axs[1,0].set_ylim((-1.5,1.5))
  94. axs[1,0].set_yticks(np.arange(-1.5, 1.51, 0.5))
  95. axs[1,0].set_xlim(right=8000)
  96. axs[1,0].set_xticks(np.arange(0, 8001, 1000))
  97. axs[1,0].grid(True, axis='both', which='both', linestyle='--', linewidth=0.5)
  98.  
  99. # Plot distance vs time
  100. axs[1,1].plot(np.arange(0, t, 1), list(zip(*data))[1], 'm-')
  101. axs[1,1].set_xlabel('Time [s]')
  102. axs[1,1].set_ylabel('Distance [km]')
  103. axs[1,1].set_ylim(top=17500)
  104. axs[1,1].set_yticks(np.arange(0, 17501, 2500))
  105. axs[1,1].set_xlim(right=8000)
  106. axs[1,1].set_xticks(np.arange(0, 8001, 1000))
  107. axs[1,1].grid(True, axis='both', which='both', linestyle='--', linewidth=0.5)
  108.  
  109. plt.tight_layout()
  110. plt.subplots_adjust(top=0.9)
  111. plt.savefig("distance_london_sydney.png", dpi=300)
  112. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement