sympy:根据其他功能解决功能问题

时间:2014-05-01 21:05:49

标签: python sympy equation-solving

我有同情系统:

m1, m2 = symbols('m1, m2', real=True, positive=True)
t0, t = symbols('t0 t', real=True, positive=True)
p1, p2, v1, v2, a1, a2, f1, f2 = symbols('p1 p2 v1 v2 a1 a2 f1 f2', cls=Function)

G = 6.67384e-11

eq1 = Eq(f1(t), G * m1 * m2 * (p2(t) - p1(t)) / abs(p2(t) - p1(t)))
eq2 = Eq(a1(t), a1(t0) + (1/m1) * Integral(f1(t), (t, t0, t)))
eq3 = Eq(v1(t), v1(t0) + Integral(a1(t), (t, t0, t)))
eq4 = Eq(p1(t), p1(t0) + Integral(v1(t), (t, t0, t)))

eq5 = Eq(f2(t), G * m1 * m2 * (p1(t) - p2(t)) / abs(p2(t) - p1(t)))
eq6 = Eq(a2(t), a2(t0) + (1/m2) * Integral(f2(t), (t, t0, t)))
eq7 = Eq(v2(t), v2(t0) + Integral(a2(t), (t, t0, t)))
eq8 = Eq(p2(t), p2(t0) + Integral(v2(t), (t, t0, t)))

我希望用t来解释p1(t)和p2(t)的这些方程式,但是如果我用它:

results = solve([eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8],
                [p1(t), p2(t)])

我只是按照v1(t)和v2(t)得到p1(t)和p2(t2):

p1(t0) + Integral(v1(t), (t, t0, t))
p2(t0) + Integral(v2(t), (t, t0, t))

如果我使用它:

results = solve([eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8],
                [p1(t), p2(t), v1(t), v2(t), a1(t), a2(t), f1(t), f2(t)])

我明白了:

p1(t0) + Integral(v1(t0) + Integral(a1(t), (t, t0, t)), (t, t0, t))
p2(t0) + Integral(v2(t0) + Integral(a2(t), (t, t0, t)), (t, t0, t))

(我省略了其他功能的结果,因为它们很长。)

如何以纯粹的t来计算p1(t)和p2(t)的结果?

1 个答案:

答案 0 :(得分:0)

尝试将(p2(t) - p1(t)) / abs(p2(t) - p1(t)))替换为Symbol('s'),将(p1(t) - p2(t)) / abs(p2(t) - p1(t)))替换为-Symbol('s')。然后重新解决感兴趣的变量,并在必要时进行后备:

>>> from sympy.utilities.misc import filldedent
>>> def backsub(sol):
...     for i in range(len(sol)):
...         was = sol.copy()
...         for k in sol.keys():
...             sol[k] = sol[k].subs(sol)
...         if was == sol:
...             break
...
>>> v
(p1, p2, v1, v2, a1, a2, f1, f2)
>>> v=[i(t) for i in _]
>>> sol=solve(eqs,v);backsub(sol)
>>> print filldedent(sol[p1(t)].doit().simplify())

1.11230666666667e-11*m2*s*t**3 - 3.33692e-11*m2*s*t**2*t0 +
3.33692e-11*m2*s*t*t0**2 - 1.11230666666667e-11*m2*s*t0**3 +
0.5*t**2*a1(t0) - 1.0*t*t0*a1(t0) + 1.0*t*v1(t0) + 0.5*t0**2*a1(t0) -
1.0*t0*v1(t0) + 1.0*p1(t0)
>>> print filldedent(sol[p2(t)].doit().simplify())

-1.11230666666667e-11*m1*s*t**3 + 3.33692e-11*m1*s*t**2*t0 -
3.33692e-11*m1*s*t*t0**2 + 1.11230666666667e-11*m1*s*t0**3 +
0.5*t**2*a2(t0) - 1.0*t*t0*a2(t0) + 1.0*t*v2(t0) + 0.5*t0**2*a2(t0) -
1.0*t0*v2(t0) + 1.0*p2(t0)