Guest User

Untitled

a guest
Nov 13th, 2025
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.57 KB | None | 0 0
  1. red_car_mass := 251.8~0.05 "CAR 1"
  2. blue_car_mass := 254.0~0.05 "CAR 2"
  3. m1_mass := 252.4~0.05
  4. m2_mass := 254.2~0.05
  5.  
  6. slopes := [0.0186~0.00015, 0.0213~0.00025, 0.0259~0.00024, 0.0187~0.00069, 0.0225~0.00025]
  7.  
  8. define extract_column(matrix, col) as
  9. column := []
  10.  
  11. for row in matrix loop
  12. column | row[col]
  13. end loop
  14.  
  15. return column
  16. end extract_column
  17.  
  18. define extract_columns(matrices, col) as
  19. return ((x) -> extract_column(x, col))`(matrices)
  20. end extract_columns
  21.  
  22. define flatten(matrix) as
  23. result := []
  24.  
  25. for set in matrix loop
  26. result.extend(set)
  27. end loop
  28.  
  29. return result
  30. end flatten
  31.  
  32. define sum(x, y) as
  33. return x + y
  34. end sum
  35.  
  36. define sums(x, y) as
  37. return sum`(x, y)
  38. end sums
  39.  
  40.  
  41. print("The standard deviation of our measurements is:", std(slopes))
  42.  
  43.  
  44. "=== Elastic data ==="
  45.  
  46. "[beginning_pos, cart2_v_at, cart1_v_after, cart2_v_after]"
  47. elastic_collisions_data_massless := [
  48. [1.6, 0.312249, -0.289780, -0.005616],
  49. [2.070, 0.343593, -0.323470, -0.002712],
  50. [1.94, 0.365498, -0.313415, -0.001286],
  51. [1.26, 0.688140, -0.708082, 0.008666],
  52. [2.04, 0.205810, -0.186434, -0.011421]
  53. ]
  54.  
  55. "[beginning_pos, cart2_v_at, cart1_v_after, cart2_v_after]"
  56. elastic_collisions_data_massed := [
  57. [1.3, 0.338302, -0.330059, -0.003630],
  58. [0.860, 0.383199, -0.34254, 0.001001],
  59. [1.260, 0.260496, -0.256836, -0.006353],
  60. [1.270, 0.472937, -0.460785, -0.002867],
  61. [1.260, 0.574692, -0.559910, 0.0001650842]
  62. ]
  63.  
  64. "[beginning_pos, cart2_v_at, cart1_v_after, cart2_v_after]"
  65. elastic_collisions_data_c1m1 := [
  66. [1.26, 0.447398, -0.284034, -0.14181],
  67. [1.08, 0.443326, -0.272659, -0.134786],
  68. [1.34, 0.435538, -0.266906, -0.146262],
  69. [1.4, 0.376664, -0.244017, -0.118208]
  70. ]
  71.  
  72. "[beginning_pos, cart2_v_at, cart1_v_after, cart2_v_after]"
  73. elastic_collisions_data_c1m2 := [
  74. [1.7, 0.596225, -0.289686, -0.300989],
  75. [0.7, 0.59995, -0.278186, -0.267378],
  76. [0.68, 0.785811, -0.370249, -0.351386],
  77. [1.72, 0.492266, -0.220981, -0.233584]
  78. ]
  79.  
  80. "[beginning_pos, cart2_v_at, cart1_v_after, cart2_v_after]"
  81. elastic_collisions_data_c2m1 := [
  82. [0.71, 0.302166, -0.41089, 0.089709],
  83. [1.640, 0.220673, -0.300117, 0.061775],
  84. [0.880, 0.307913, -0.418068, 0.100391],
  85. [0.580, 0.35472, -0.446097, 0.117493]
  86. ]
  87.  
  88. "[beginning_pos, cart2_v_at, cart1_v_after, cart2_v_after]"
  89. elastic_collisions_data_c2m2 := [
  90. [1.32, 0.197016, -0.268901, 0.083176],
  91. [1.19, 0.26225, -0.321149, 0.111764],
  92. [1.46, 0.229498, -0.303468, 0.094574],
  93. [1.33, 0.203483, -0.286996, 0.086106]
  94. ]
  95.  
  96.  
  97. "=== Inelastic data ==="
  98.  
  99. "[beginning_pos, cart2_v_at, cart1_v_after]"
  100. inelastic_collisions_data_massless := [
  101. [1.43, 0.392352, -0.196709],
  102. [1.35, 0.496739, -0.241296],
  103. [1.08, 0.69781, -0.337878],
  104. [1.03, 0.489847, -0.239846]
  105. ]
  106.  
  107. "[beginning_pos, cart2_v_at, cart1_v_after]"
  108. inelastic_collisions_data_c1m1 := [
  109. [0.960, 0.588607, -0.202218],
  110. [1.88, 0.407084, -0.14504],
  111. [1.14, 0.580273, -0.189352]
  112. ]
  113.  
  114. "[beginning_pos, cart2_v_at, cart1_v_after]"
  115. inelastic_collisions_data_c1m2 := [
  116. [0.96, 0.851525, -0.132017],
  117. [2.28, 0.558863, -0.136012],
  118. [2.1, 0.632977, -0.153357]
  119. ]
  120.  
  121. "[beginning_pos, cart2_v_at, cart1_v_after]"
  122. inelastic_collisions_data_c2m1 := [
  123. [2.08, 0.442954, -0.294568],
  124. [2.56, 0.47818, -0.306290],
  125. [1.26, 0.460557, -0.315441]
  126. ]
  127.  
  128. "[beginning_pos, cart2_v_at, cart1_v_after]"
  129. inelastic_collisions_data_c2m2 := [
  130. [1.84, 0.56718, -0.393595],
  131. [1.78, 0.413264, -0.31968],
  132. [1.74, 0.492195, -0.38198]
  133. ]
  134.  
  135. all_data := [
  136. elastic_collisions_data_massless,
  137. elastic_collisions_data_c1m1,
  138. elastic_collisions_data_c1m2,
  139. elastic_collisions_data_c2m1,
  140. elastic_collisions_data_c2m2,
  141. inelastic_collisions_data_massless,
  142. inelastic_collisions_data_c1m1,
  143. inelastic_collisions_data_c1m2,
  144. inelastic_collisions_data_c2m1,
  145. inelastic_collisions_data_c2m2
  146. ]
  147.  
  148.  
  149. define calc_kinetic(mass, velocity) as
  150. return 0.5 * mass * velocity ^ 2
  151. end calc_kinetic
  152.  
  153. define calc_momentum(mass, velocity) as
  154. return mass * velocity
  155. end calc_momentum
  156.  
  157.  
  158. define negate_car1v(list) as
  159. for row in list loop
  160. row[2] := -1 * row[2]
  161. end loop
  162. end negate_car1v
  163.  
  164.  
  165. "Since the sensors were in different directions, we needed to negate one of them"
  166. "We chose to negate the one pointing towards car 1"
  167. negate_car1v`(all_data)
  168.  
  169. from physics_utils.graph import SimpleGraph, show
  170.  
  171. "From the velocities of the carts, calculate the momenta and kinetic energies. Plot the total kinetic energy before the"
  172. "collision vs the total kinetic energy after collision. Is kinetic energy conserved (i.e. is the collision 'elastic')?"
  173. "What slope would this correspond to? Also, plot the momenta before collision vs momenta after the"
  174. "collision. Is momentum conserved?"
  175.  
  176. elastic_datasets := [
  177. elastic_collisions_data_massless,
  178. elastic_collisions_data_c1m1,
  179. elastic_collisions_data_c1m2,
  180. elastic_collisions_data_c2m1,
  181. elastic_collisions_data_c2m2
  182. ]
  183.  
  184. elastic_masses_c1 := [
  185. red_car_mass,
  186. red_car_mass + m1_mass,
  187. red_car_mass + m1_mass + m2_mass,
  188. red_car_mass,
  189. red_car_mass
  190. ]
  191.  
  192. elastic_masses_c2 := [
  193. blue_car_mass,
  194. blue_car_mass,
  195. blue_car_mass,
  196. blue_car_mass + m1_mass,
  197. blue_car_mass + m1_mass + m2_mass
  198. ]
  199.  
  200.  
  201.  
  202. elastic_c1_velocities_after := extract_columns(elastic_datasets, 2)
  203. elastic_c2_velocities_before := extract_columns(elastic_datasets, 1)
  204. elastic_c2_velocities_after := extract_columns(elastic_datasets, 3)
  205.  
  206. "First, let's calculate the momenta for each dataset:"
  207.  
  208. print()
  209.  
  210. define calc_momenta_on_dataset(mass, velocities) as
  211. return ((x) -> calc_momentum(mass, x))`(velocities)
  212. end calc_momenta_on_dataset
  213.  
  214. cart1_momenta_after := calc_momentum**(*elastic_masses_c1, **elastic_c1_velocities_after)
  215. cart2_momenta_before := calc_momentum**(*elastic_masses_c2, **elastic_c2_velocities_before)
  216. cart2_momenta_after := calc_momentum**(*elastic_masses_c2, **elastic_c2_velocities_after)
  217.  
  218. total_momenta_before := cart2_momenta_before
  219. total_momenta_after := sums`(cart2_momenta_after, cart1_momenta_after)
  220. elastic_total_momenta_before := total_momenta_before
  221. elastic_total_momenta_after := total_momenta_after
  222.  
  223. print("Total momenta before:", total_momenta_before)
  224. print("Total momenta after:", total_momenta_after)
  225.  
  226. "& graph it:"
  227.  
  228. momenta_graph := SimpleGraph("Momentum before vs after elastic collision")
  229. momenta_graph.set_x_axis(flatten(total_momenta_after), "Total momentum after (g m / s)")
  230. momenta_graph.set_y_axis(flatten(total_momenta_before), "Total momentum before (g m / s)")
  231.  
  232. momenta_graph.plot_points()
  233. momenta_graph.put_labels()
  234.  
  235. line_info := momenta_graph.best_fit(true)
  236.  
  237. print("Line of best fit for elastic momenta graph:", "$y=({})x+{}$".format(line_info[0].latex(false), line_info[1].latex(false)))
  238.  
  239. "Now, let's calculate the kinetic energy:"
  240.  
  241. print()
  242.  
  243. define calc_ke_on_dataset(mass, velocities) as
  244. return ((x) -> calc_kinetic(mass, x))`(velocities)
  245. end calc_ke_on_dataset
  246.  
  247. cart1_ke_after := calc_ke_on_dataset`(elastic_masses_c1, elastic_c1_velocities_after)
  248. cart2_ke_before := calc_ke_on_dataset`(elastic_masses_c2, elastic_c2_velocities_before)
  249. cart2_ke_after := calc_ke_on_dataset`(elastic_masses_c2, elastic_c2_velocities_after)
  250.  
  251. total_ke_before := cart2_ke_before
  252. total_ke_after := sums`(cart2_ke_after, cart1_ke_after)
  253. elastic_total_ke_before := total_ke_before
  254. elastic_total_ke_after := total_ke_after
  255.  
  256. print("Total kinetic energy before:", total_ke_before)
  257. print("Total kinetic energy after:", total_ke_after)
  258.  
  259. "& graph it:"
  260.  
  261. ke_graph := SimpleGraph("Kinetic energy before vs after elastic collision")
  262. ke_graph.set_x_axis(flatten(total_ke_after), "Total kinetic energy after (g m^2 / s^2)")
  263. ke_graph.set_y_axis(flatten(total_ke_before), "Total kinetic energy before (g m^2 / s^2)")
  264.  
  265. ke_graph.plot_points()
  266. ke_graph.put_labels()
  267.  
  268. line_info := ke_graph.best_fit(true)
  269.  
  270. print("Line of best fit for elastic kinetic graph:", "$y=({})x+{}$".format(line_info[0].latex(false), line_info[1].latex(false)))
  271.  
  272. "Ok now we're doing the stuff for the inelastic collision"
  273.  
  274. "you will find each cart's velocity before the collision, and the velocity of the"
  275. "combined system of two carts after collision (because they're stuck together). Once again, compare"
  276. "the momenta and kinetic energies before and after the collision. Is this collision elastic? Is kinetic"
  277. "energy conserved? Is momentum conserved? If one was not conserved, what happened to it?"
  278.  
  279. inelastic_datasets := [
  280. inelastic_collisions_data_massless,
  281. inelastic_collisions_data_c1m1,
  282. inelastic_collisions_data_c1m2,
  283. inelastic_collisions_data_c2m1,
  284. inelastic_collisions_data_c2m2
  285. ]
  286.  
  287. inelastic_masses_c1 := [
  288. red_car_mass,
  289. red_car_mass + m1_mass,
  290. red_car_mass + m1_mass + m2_mass,
  291. red_car_mass,
  292. red_car_mass
  293. ]
  294.  
  295. inelastic_masses_c2 := [
  296. blue_car_mass,
  297. blue_car_mass,
  298. blue_car_mass,
  299. blue_car_mass + m1_mass,
  300. blue_car_mass + m1_mass + m2_mass
  301. ]
  302.  
  303. inelastic_c1_velocities_after := extract_columns(inelastic_datasets, 2)
  304. inelastic_c2_velocities_before := extract_columns(inelastic_datasets, 1)
  305. inelastic_c2_velocities_after := extract_columns(inelastic_datasets, 2)
  306.  
  307. "Let's calculate the momenta:"
  308.  
  309. print()
  310.  
  311. total_momenta_before := calc_momenta_on_dataset`(inelastic_masses_c2, inelastic_c2_velocities_before)
  312. total_momenta_after := calc_momenta_on_dataset`(sums(inelastic_masses_c2, inelastic_masses_c1), inelastic_c2_velocities_after)
  313. inelastic_total_momenta_before := total_momenta_before
  314. inelastic_total_momenta_after := total_momenta_after
  315.  
  316. print("Total momenta before:", total_momenta_before)
  317. print("Total momenta after:", total_momenta_after)
  318.  
  319. "& graph it:"
  320.  
  321. momenta_graph := SimpleGraph("Momentum before vs after inelastic collision")
  322. momenta_graph.set_x_axis(flatten(total_momenta_after), "Total momentum after (g m / s)")
  323. momenta_graph.set_y_axis(flatten(total_momenta_before), "Total momentum before (g m / s)")
  324.  
  325. momenta_graph.plot_points()
  326. momenta_graph.put_labels()
  327.  
  328. line_info := momenta_graph.best_fit(true)
  329.  
  330. print("Line of best fit for inelastic momentum graph:", "$y=({})x+{}$".format(line_info[0].latex(false), line_info[1].latex(false)))
  331.  
  332. "Now, let's calculate the kinetic energy:"
  333.  
  334. print()
  335.  
  336. cart1_ke_after := calc_ke_on_dataset`(inelastic_masses_c1, inelastic_c1_velocities_after)
  337. cart2_ke_before := calc_ke_on_dataset`(inelastic_masses_c2, inelastic_c2_velocities_before)
  338. cart2_ke_after := calc_ke_on_dataset`(inelastic_masses_c2, inelastic_c2_velocities_after)
  339.  
  340. total_ke_before := cart2_ke_before
  341. total_ke_after := sums`(cart2_ke_after, cart1_ke_after)
  342. inelastic_total_ke_before := total_ke_before
  343. inelastic_total_ke_after := total_ke_after
  344.  
  345. print("Total kinetic energy before:", total_ke_before)
  346. print("Total kinetic energy after:", total_ke_after)
  347.  
  348. "& graph it:"
  349.  
  350. ke_graph := SimpleGraph("Kinetic energy before vs after inelastic collision")
  351. ke_graph.set_x_axis(flatten(total_ke_after), "Total kinetic energy after (g m^2 / s^2)")
  352. ke_graph.set_y_axis(flatten(total_ke_before), "Total kinetic energy before (g m^2 / s^2)")
  353.  
  354. ke_graph.plot_points()
  355. ke_graph.put_labels()
  356.  
  357. "Finally, let's see our graphs:"
  358. show()
  359.  
  360. print()
  361. print("Now, for our tables")
  362. print()
  363.  
  364. from physics_utils.table import Table2D
  365.  
  366. trial_num := [0]
  367.  
  368. define make_row(m1, m2, run) as
  369. trial_num[0] := trial_num[0] + 1
  370. return [trial_num[0].__int__(), m1, m2, run[1], run[2], run[3]]
  371. end make_row
  372.  
  373. table_data := flatten(((m1, m2, runs) -> ((run) -> make_row(m1, m2, run))`(runs))`(elastic_masses_c1, elastic_masses_c2, elastic_datasets))
  374.  
  375. elastic_collision_table := Table2D()
  376.  
  377. elastic_collision_table.set_labels(["$m_1$", "$m_2$", "$v_{i_2}$", "$v_{f_1}$", "$v_{f_2}$"])
  378. elastic_collision_table.set_data(table_data)
  379.  
  380. "print(elastic_collision_table.latex())"
  381.  
  382.  
  383. define make_row(m1, m2, run) as
  384. trial_num[0] := trial_num[0] + 1
  385. return [trial_num[0].__int__(), m1, m2, run[1], run[2]]
  386. end make_row
  387.  
  388.  
  389.  
  390. table_data := flatten(((m1, m2, runs) -> ((run) -> make_row(m1, m2, run))`(runs))`(inelastic_masses_c1, inelastic_masses_c2, inelastic_datasets))
  391.  
  392. inelastic_collision_table := Table2D()
  393.  
  394. inelastic_collision_table.set_labels(["$m_1$", "$m_2$", "$v_{i_2}$", "$v_{f_1}$"])
  395. inelastic_collision_table.set_data(table_data)
  396.  
  397. "print(inelastic_collision_table.latex())"
  398.  
  399. trial_num[0] := 0
  400.  
  401. define make_row(momenta_before, momenta_after, ke_before, ke_after) as
  402. trial_num[0] := trial_num[0] + 1
  403. return [trial_num[0].__int__(), momenta_before, momenta_after, ke_before, ke_after]
  404. end make_row
  405.  
  406. table_data := flatten(make_row**(**elastic_total_momenta_before, **elastic_total_momenta_after, **elastic_total_ke_before, **elastic_total_ke_after))
  407.  
  408. elastic_calc_table := Table2D()
  409.  
  410. elastic_calc_table.set_labels(["Trial", "$p_i$", "$p_f$", "k_{ei}", "k_{ef}"])
  411. elastic_calc_table.set_data(table_data)
  412.  
  413. "print(elastic_calc_table.latex())"
  414.  
  415.  
  416. table_data := flatten(make_row**(**inelastic_total_momenta_before, **inelastic_total_momenta_after, **inelastic_total_ke_before, **inelastic_total_ke_after))
  417. inelastic_calc_table := Table2D()
  418.  
  419. inelastic_calc_table.set_labels(["Trial", "$p_i$", "$p_f$", "k_{ei}", "k_{ef}"])
  420. inelastic_calc_table.set_data(table_data)
  421.  
  422. print(inelastic_calc_table.latex())
Advertisement
Add Comment
Please, Sign In to add comment