求解 Python 中的物理方程

Solving a physics equation in Python

我是 Python 的新手,我想到了编写一个程序来求解物理学中使用的运动方程。

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

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

我将此代码用于所需的每个值(位移、初始速度、最终速度、加速度和时间)

如果用户输入 / 作为值,它将不会用在方程中,所以我写了一个小赋值器来决定使用哪个方程。

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

初始速度 (vi) 用于每个方程式,因此我不需要为此添加一个。

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)

我的问题是,如何将其他变量的值插入到计算机可以求解的方程中,然后打印出来?

这似乎是一个基本问题,但我不确定如何用 Python.

做这样的代数
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)

要求解 python 中的方程 d = Vi*t + 1/2*a*t^2,您需要

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

这是如何工作的?

  1. 初始速度乘以时间
  2. 将 1/2 乘以 a
  3. 将该数量(第 2 步)乘以 t
  4. 的平方

对于其他方程,你真的想求解一个变量,所以:

t = (Vf-Vi)/a

虽然我是编码新手,但以下代码可能会解决问题:

此函数求解一个运动方程[ d = vi*t + 1/2*a*t**2 ],变量为位移(d)、初速度(vi)、加速度(a)、时间(t)。

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

print (d)

""" 通过输入 vi、t 和 a 的值来调用函数。 您可以更改自己的值。"""

eq1(3,4,5)    

对于具有相同变量的其他方程式,您需要将方程式放在 eq1 之后,例如 d1="......" 并打印 (d1) 或对于方程式中的更多变量,定义另一个具有所有的函数变量如- def eq2(vi, t,a, x).