Alex Brown
Alex Brown

Reputation: 21

Solving a physics equation in Python

I'm fairly new to Python, and I got an idea to write a program to solve the Kinematic Equations used in physics.

vi = input("What is the initial velocity?")

if vi == "/":
    dontuse = "vi"
else:
    pass

I used this code for each of the values needed (Displacement, Initial Velocity, Final Velocity, Acceleration, and Time)

If the user inputs / as the value, it will not be used in the equation, so I wrote a small assignor to decide which equation to use.

if dontuse == "a":
    eq3()
elif dontuse == "d":
    eq4()
elif dontuse == "vf":
    eq1()
elif dontuse == "t":
    eq2()

Initial Velocity (vi) is used in every equation, so I didnt need to add one for that.

def eq1():
    # d = Vi*t + 1/2*a*t^2
    print("Equation 1!")
    answer = # d = Vi*t + 1/2*a*t^2
    print("Your answer is:", answer)

My question is here, how would I insert the values of the other variables into an equation that the comupter could solve, then print out?

It may seem like a basic question, but I wasnt sure how to do algebra like this with with Python.

Upvotes: 0

Views: 9083

Answers (2)

Geom
Geom

Reputation: 1

Though am a newbie in coding, the following code might solve the problem:

This function is to solve one kinematic equation [ d = vi*t + 1/2*a*t**2 ], having Displacement (d), Initial Velocity(vi), Acceleration (a), and Time (t) as variables.

def eq1( vi, t, a):
d= vi * t + 1/2 *a * t **2

print (d)

""" calling the function by putting the values of vi, t, and a. you can change the values of your own."""

eq1(3,4,5)    

For other equations with same variables, you need to put the equations after eq1 like d1="......." and print (d1) or for more variables in a equation, define another function having all variables like- def eq2(vi, t,a, x).

Upvotes: 0

intboolstring
intboolstring

Reputation: 7100

def eq1():
    # d = Vi*t + 1/2*a*t^2
    print("Equation 1!")
    answer = # d = Vi*t + 1/2*a*t^2
    print("Your answer is:", answer)

To solve the equation d = Vi*t + 1/2*a*t^2in python, you would do

answer = Vi*t + .5*a*(t**2)

How does this work??

  1. Multiplies initial velocity by time
  2. Multiplies 1/2 by a
  3. Multiplies that quantity (step 2) by the square of t

For the other equations, you really want to solve for one variable, so:

t = (Vf-Vi)/a

Upvotes: 1

Related Questions