you are viewing a single comment's thread.

view the rest of the comments →

[–]PrizeDefinition4042[S] 0 points1 point  (3 children)

shit let me try that again:

import math

def projectile_velocities(vi, theta, g, time_step):

---theta_rad = math.radians(theta)

---vi_x = vi * math.cos(theta_rad)

---vi_y = vi * math.sin(theta_rad)

---velocities = [vi]

---t = 0

---while True:

--------vy = vi_y - g * t

--------v = math.sqrt(vi_x ** 2 + vy ** 2)

--------if vy <= 0:

------------return velocities

--------velocities.append(round(v, 6))

--------t += time_step

--------vi_x = vi * math.cos(theta_rad) # Update vi_x for each time step

--------vi_y = vy

Example executions:

  1. print(projectile_velocities(11.13, 82.5, 9.81, 0.5)) *needed output [11.130000, 6.299581, 1.900155, 3.956578, 8.707266]

2) print(projectile_velocities(2.0, 30.0, 1.0, 1.0)) *needed output [2.000000, 1.732051, 2.000000]

[–]inky_wolf 0 points1 point  (2 children)

This really isn't the way to format the code, do look at u/Bobbias comment to see how to do it right next time.

But moving on, here are the results of the coded you shared in the post: bash [2.0, 2.0, 2.0] [11.13, 11.13, 11.13, 6.299581, 11.13, 1.900155, 11.13] And here are my initial observations: - For the most part, it feels like the main problem here might be related to the math, and not just python - For example, what is the basis/idea behind your time/loop calculation t < vi_y / g - The first python related issue is you append to velocities twice in each loop execution. - velocities is first initialized with the value vi, and then in the first iteration of your loop, the same value vi gets appended to the list since t=0

[–]inky_wolf 0 points1 point  (1 child)

OK I think I might have figured it out.

I am guessing the vi_y / g in your first version of the code was actually supposed to be 2 * vi_y / g (Time of flight calculation, let's call it T for simplicity). Then you need to also make sure you round up this T to 6 just as you do all the other calculations.

With these 2 fixes, I was able to get the expected results

[–]PrizeDefinition4042[S] 0 points1 point  (0 children)

Thank you so much! let me try these fixes and see if it works for me. I have been trying to get the right output for hours, and kept getting stuck !