Home > Net >  trying to translate a math formula into python
trying to translate a math formula into python

Time:02-13

I'm rather new to python I'm writing this program that calculates the falling time of a box using this simple formula:

h-0.5gT^2 = 0

h = 100
g = 9.8
T = ?

Where T is the time it takes (4.5 sec) On paper, I can easily solve for T but I'm not sure how to translate this into python I don't know if there's a formula that can give me the value of T with the inputs I already have or what

CodePudding user response:

import math
T = math.sqrt(h / (0.5 * g))

CodePudding user response:

Based on this you could use the following code:

from sympy import symbols, solve

T = symbols('T')
h=100
g=9.8

expr = h-0.5*g*(T*T)

sol=solve(expr)
print(sol)

As an alternative you do the operations yourself by writing (as @yair koskas wrote):

T = math.sqrt(h / (0.5 * g))

CodePudding user response:

If I understand the question correctly, you are asking how to solve for T.

T = ((2 * h) / g) ** 0.5

So start Python interpreter and type the following

h = 100
g = 9.81
T = ((2 * h) / g) ** 0.5
print(T)
  • Related