Python Forum
Numerically determining the highest a ball travels when shot straight up - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Numerically determining the highest a ball travels when shot straight up (/thread-2952.html)



Numerically determining the highest a ball travels when shot straight up - JakeWitten - Apr-21-2017

Hi all, I'm very new to python and I have no prior experience with other coding languages, so my main issues are with the formatting and finding the appropriate vocabulary. 

My goal is to determine the maximum height a ball reaches when thrown straight up.  I already wrote the code for the visual of the ball, but I don't know how to find and display a maximum value.  Any other advice or suggestions in regards to the code I already have would also be greatly appreciated.

Thanks!  Smile

from visual import *

myuniv = display(range = vector(3,3,3))

So = vector(0,1,0)
Vo = vector(0,6,0)
a = vector(0,-9.8,0)
s = vector(0,2,0)

dt = 0.1
t = 0.0

ball = sphere(pos = s, radius = 0.25, color = color.cyan)

while s.y > -2.0:
    rate(25)
    s = .5*a*t**2 + Vo*t + So
    v = a*t + Vo
    t = t + dt
    ball.pos = s
    ballghost = sphere(pos = s, radius = .2)



RE: Numerically determining the highest a ball travels when shot straight up - nilamo - Apr-21-2017

I'm guessing s is the current height, since ball.pos = s.  In that case, just keep track of s.

Something like
max_pos = 0
while s.y > -2.0:
   s = #... calculate s
   if s > max_pos:
       max_pos = s



RE: Numerically determining the highest a ball travels when shot straight up - JakeWitten - Apr-21-2017

Thanks! This worked out perfectly.  I wasn't sure how to set up an "if" statement before.  Big Grin


RE: Numerically determining the highest a ball travels when shot straight up - Ofnuts - Apr-22-2017

Was you get is not the maximum height of the ball, but the maximum height you compute, and the accuracy therefore depends on the size of your samples. Getting the real value isn't that difficult (t for which v==0 so tmax=-Vo/a).