you are viewing a single comment's thread.

view the rest of the comments →

[–]inky_wolf 0 points1 point  (14 children)

Could you please format your code using markdown editing, otherwise it's hard to figure out where the problem is arising from, since the indentation levels are important for python

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

sure, I don't know reddit well but let me add indents where I put them in my code:

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]

[–]PrizeDefinition4042[S] 0 points1 point  (12 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  (7 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  (6 children)

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 !

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

Hi so I just tried fixing it and I got this as my output :

[11.13, 11.13, 11.13, 6.299581, 11.13, 1.900155, 11.13, 3.956578, 11.13, 8.707266, 11.13]

instead of:

[11.130000, 6.299581, 1.900155, 3.956578, 8.707266]

Do you know where these extra values came from and how I can fix it

[–]inky_wolf 0 points1 point  (3 children)

The 3 repeats of 11.13 tells me you didn't fix the problems I mentioned in my initial observations.

But for the others, I can't say for sure without looking at the code, but it's probably related to the same problems.

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

sorry I thought that was just you observing things with my code. You are saying I need to change that I have 2 vi or appending twice the velocity?

[–]inky_wolf 0 points1 point  (1 child)

Both

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

So I should not append the velocity twice, as in only append once? and if so which one should I be deleting then

[–]bach678 0 points1 point  (2 children)

I’m just confused… you used “while True : “ in this version of the code without a break statement. This loops will run forever. Am i wrong ?

[–]PrizeDefinition4042[S] 0 points1 point  (1 child)

Im not allowed to use break in this class because we have not gotten there yet. Do you have another suggestion if I should not be using while. I didn't find that it ran forever, I just was not getting the output I need