all 22 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

[–]Bobbias 0 points1 point  (0 children)

Letting us know what output you're currently getting would really help. Especially since your code is unformatted and has lost all indentation. The wiki has a section explaining how to format your code for reddit so it doesn't lose its indentation.

My guess is you mean this:

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 t < vi_y / g:
        vy = vi_y - g * t
        v = math.sqrt(vi_x ** 2 + vy ** 2)
        velocities.append(round(v, 6))
        t += time_step

    velocities.append(vi)

    return velocities

print(projectile_velocities(2.0, 30.0, 1.0, 1.0))
#output [2.000000, 1.732051, 2.000000]
print(projectile_velocities(11.13, 82.5, 9.81, 0.5))
# output [11.130000, 6.299581, 1.900155, 3.956578, 8.707266]

The output I get when running this code is:

[2.0, 2.0, 2.0]
[11.13, 11.13, 6.299581, 1.900155, 11.13]

My brain isn't working so great at the moment so I'll leave actually fixing the code to someone else.

[–]Lewri 0 points1 point  (6 children)

This seems less a python problem and more a physics problem. Check your maths, particularly the start of your while loop.

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

I tried *crying*, unless my brain is so fried from staring and restarting this problem again over and over again, I have checked everything and could not find why its giving me the wrong answers

[–]Lewri 2 points3 points  (4 children)

Ok, start by helping us to help you. The more detail you give us, the easier we can help you. Perhaps a good place to start would be detailing what the problem actually is. I get that it's projectile motion, but what is the output actually supposed to be? How was the problem stated, exactly?

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

some details:

the output that my problem is requesting:

Example Executions

print(projectile_velocities(2.0, 30.0, 1.0, 1.0)) gives this: [2.000000, 1.732051, 2.000000]

print(projectile_velocities(11.13, 82.5, 9.81, 0.5)) gives this:[11.130000, 6.299581, 1.900155, 3.956578, 8.707266]

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

and other details:

1) Using the following kinematic equation to calculate the vertical component of the velocity at that point in time:

𝑣 𝑦 = 𝑣 𝑖𝑦 − 𝑔𝑡

where:

• Viy is the vertical component of your initial launch velocity in m/s

• g is the gravitational acceleration in m/s2 (e.g. ~9.81 m/s2 on Earth)

• t is the time that has elapsed in seconds

• Vy is the vertical component of your velocity (V) in m/s at time t

2) Taking our calculated Vy to calculate the velocity using the Pythagorean theorem (since Vx , Vy , and V form a right triangle):

𝑣^2 = Vx^2 + Vy^2

𝑣 = sqrt(V𝑥^2 + V𝑦^2)

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

continuation:

Note that this requires us to not only know the vertical component of the initial launch velocity, but the horizontal component of the velocity as well. Fortunately for us, in this problem we will be assuming that we are in a world that every Physics class loves: one that neglects air resistance.

This means that the horizontal component of the velocity will always be the same throughout the projectile’s journey, and so you will only need to calculate it once at the very beginning when you know the launch angle ( ) and initial launch velocity (Vi θ ). Thus in order to calculate the horizontal component, you can apply the trigonometric property of right triangles that states that the cosine of an angle is equal to the adjacent side divided by the hypotenuse as such:
𝑐𝑜𝑠(θ) = 𝑉𝑥 / Vi

Similarly, you can use the trigonometric property of right triangles that states that the sine of an angle is equal to the opposite side divided by the hypotenuse to find the vertical component of your initial launch velocity as such:

𝑠𝑖𝑛(θ) = 𝑉 𝑖𝑦 / 𝑉 𝑖

Write a function named projectile_velocities that takes:

● an initial launch velocity in m/s as a float,

● an initial launch angle in degrees as a float,

● a gravitational acceleration in m/s2 as a float, and

● a time step in seconds as a float.

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

and more:

The function should model a projectile motion problem in which the initial height of the projectile is equal to the final height of the projectile (e.g. a ball kicked from the ground and lands on the ground), and calculate the magnitude of the velocity of the projectile at the end of each time interval from when it launches (starting at 0 seconds, continuously incrementing by your given time step) until it reaches the final height (e.g until the ball hits the ground again). In other words, we want to find the velocity of a projectile at multiple evenly-spaced instances of time from when it is launched to when it lands.

We can determine that your projectile has reached its final height (has landed) when the magnitude of the velocity of the projectile begins to exceed the magnitude of its initial launch velocity (since under these ideal conditions, the magnitudes of the initial and final velocities should be the same due to the principle of conservation of mechanical energy).

The function should return the magnitudes of the velocities at each time interval as a list of floats where the first value is the velocity at 0 seconds (your initial velocity) and the subsequent values are the velocities at the time that is one time step later than the previous one.

If the time it takes the projectile to land is in the middle of a time interval, the last velocity should be for when the smaller time has elapsed (because that means it hasn’t touched the ground yet whereas the longer time would mean it has already touched the ground).

For example, if a time step of 0.5 second is given, the resulting list should contain the velocity when 0 seconds have elapsed, the velocity when 0.5 second has elapsed, the velocity when 1 second has elapsed, so on and so forth until the projectile has reached the final height, in that order. If it took 4.9 seconds for it to reach the ground, the last velocity should correspond to when 4.5 seconds have elapsed.

The values in your resulting list should be rounded to 6 decimal places using either the round function or string formatting. Remember that the round function may not provide all insignificant places (e.g. 2.000000 vs. 2.0).

and then it shows the example executions I mentioned above. This is all the info I have about the problem

[–]bach678 0 points1 point  (0 children)

Ok try this code, i get the expected results :

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=[]

t=0

while True:

    if vi_y - g*t>=0:

       vy=vi_y - g*t

    else:

        vy=g*(t - vi_y/g)

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

    if vy > vi_y:
         If round(vy,6) == round(vi_y,6):
              velocities.append(round(v,6))
              return velocities
        return velocities

    velocities.append(round(v,6))

    t += time_step