使用sympy.nsolve()获取“TypeError:无法创建mph”

时间:2016-09-07 10:30:22

标签: python sympy

我试图确定一些东西必须移动到与移动的椭圆相交的时间,角度和速度。 (我实际上希望这些条件持续最短时间)。现在我正试图用Sympy来帮助这次冒险。以下是我正在执行的代码:

import sympy as sp
sp.init_printing()

delta_x, delta_y, t = sp.symbols('delta_x delta_y t', real=True, positive=True)
V, V_s, x_0, y_0, theta_s, theta_t = sp.symbols('V V_s x_0 y_0 theta_s theta_t', real=True)
x, y = sp.symbols('x y', real=True)

EQ1 = sp.Eq(((x-(x_0+V*sp.cos(theta_t)*t))/(delta_x+V*sp.cos(theta_t)*t))**2+((y-(y_0+V*sp.sin(theta_t)*t))/(delta_y+V*sp.sin(theta_t)*t))**2-1, 0)
sx = sp.Eq(x, V_s*sp.cos(theta_s)*t)
sy = sp.Eq(y, V_s*sp.sin(theta_s)*t)

mysubs = [(V,5), (x_0, 10), (y_0, 10), (theta_t, 7*(sp.pi/4)), (delta_x, 0), (delta_y, 0)]
sp.nsolve((EQ1.subs(mysubs), sx.subs(mysubs), sy.subs(mysubs)), (V_s, theta_s, t), (5, 0.0, 1))

此操作的结果产生:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sympy/mpmath/calculus/optimization.py in findroot(ctx, f, x0, solver, tol, verbose, verify, **kwargs)
    927         try:
--> 928             fx = f(*x0)
    929             multidimensional = isinstance(fx, (list, tuple, ctx.matrix))

<string> in <lambda>(_Dummy_75, _Dummy_76, _Dummy_77)

/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sympy/mpmath/matrices/matrices.py in __init__(self, *args, **kwargs)
    300                     for j, a in enumerate(row):
--> 301                         self[i, j] = convert(a)
    302             else:

/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sympy/mpmath/ctx_mp_python.py in convert(ctx, x, strings)
    661             return ctx.convert(x._mpmath_(prec, rounding))
--> 662         return ctx._convert_fallback(x, strings)
    663 

/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sympy/mpmath/ctx_mp.py in _convert_fallback(ctx, x, strings)
    613                 raise ValueError("can only create mpf from zero-width interval")
--> 614         raise TypeError("cannot create mpf from " + repr(x))
    615 

TypeError: cannot create mpf from 0.08*(x - 13.535533905932737622)**2 + 0.08*(y - 6.464466094067262378)**2 - 1

这是因为系统不够受限制吗?有一系列角度和速度可用于拦截移动的椭圆。这个错误似乎并不意味着这一点。 (是的,即使我尝试在此问题中约束V_s,也会出现相同的错误。)

我使用的是以下版本的内容:

| Software | Version   |
|----------|-----------|
| python   | 3.5.2     |
| sympy    | 1.0       |
| mpmath   | 0.19      |

1 个答案:

答案 0 :(得分:0)

变量xy在方程式中仍然是符号。 nsolve要求指定所有变量,并且至少需要与变量一样多的等式。因此,您需要在mysubs中包含它们的值,或者为它们求解(但要使用nsolve执行此操作,您将需要另外两个等式)。