无法将表达式转换为浮点数?

时间:2018-04-22 21:07:11

标签: python

我试图用python解决9个非线性方程,但输出是错误的。我发布的代码如下: 该代码已将P1 P2 P3 A1 A2 A3解析为存储的[x,y,z]坐标数。我试图用这些位置来找出最后三个点(x1,y1,z1),(x2,y2,z2),(x3,y3,z3)。

# For symbolic calculations use sympy
import sympy
import scipy
from scipy.optimize import fsolve
import math
import numpy as np
from numpy import cos,sin

###Set up B1 point 
x1, y1, z1 = sympy.symbols('x1 y1 z1')

###Set up B2 point 
x2, y2, z2 = sympy.symbols('x2 y2 z2')

### Set up B3 point 
x3, y3, z3 = sympy.symbols('x3 y3 z3')

# Each lower arm has length Lc, this is length for A B  
Lc = 3.0 

#Define second points into seperate variables 
a11 = A1[0]
a12 = A1[1]
a13 = A1[2]
a21 = A2[0]
a22 = A2[1]
a23 = A2[2]
a31 = A3[0]
a32 = A3[1]
a33 = A3[2]

##Constrain for Bar AB
eq1 = (x1 - A1[0])**2 + (y1 - A1[1])**2 + (z1 - A1[2])**2 - Lc**2 
eq2 = (x2 - A2[0])**2 + (y2 - A2[1])**2 + (z2 - A2[2])**2 - Lc**2 
eq3 = (x3 - A3[0])**2 + (y3 - A3[1])**2 + (z3 - A2[2])**2 - Lc**2 




### Length for plate B points
Ld = 0.2

##Constrain for plate B points
eq4 = (x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2 - Ld**2 
eq5 = (x3 - x1)**2 + (y3 - y1)**2 + (z3 - z1)**2 - Ld**2 
eq6 = (x3 - x2)**2 + (y3 - y2)**2 + (z3 - z2)**2 - Ld**2 

###Vectors for points P and points A

Upa1 = [A1[0] - P1[0], A1[1] - P1[1], A1[2] - P1[2] ]
Upa2 = [A2[0] - P2[0], A2[1] - P2[1], A2[2] - P2[2] ]
Upa3 = [A3[0] - P3[0], A3[1] - P3[1], A3[2] - P3[2] ]

###Vectors for points A and B
Uab1 = [x1 - A1[0], y1 - A1[1], z1 - A1[2]]
Uab2 = [x2 - A2[0], y2 - A2[1], z2 - A2[2]]
Uab3 = [x3 - A3[0], y3 - A3[1], z3 - A3[2]]
eq7 = np.dot(np.cross(Upa1, Uab1),u1)
eq8 = np.dot(np.cross(Upa2, Uab2),u2)
eq9 = np.dot(np.cross(Upa3, Uab3),u3)

###Using Scipy to solve equations 
def equations(z):
  x1 = z[0]
  y1 = z[1]
  z1 = z[2]
  x2 = z[3]
  y2 = z[4]
  z2 = z[5]
  x3 = z[6]
  y3 = z[7]
  z3 = z[8]

  F = empty((9))
  F[0] = (x1 - A1[0])**2 + (y1 - A1[1])**2 + (z1 - A1[2])**2 - Lc**2
  F[1] = (x2 - A2[0])**2 + (y2 - A2[1])**2 + (z2 - A2[2])**2 - Lc**2 
  F[2] = (x3 - A3[0])**2 + (y3 - A3[1])**2 + (z3 - A2[2])**2 - Lc**2 
  F[3] = (x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2 - Ld**2 
  F[4] = (x3 - x1)**2 + (y3 - y1)**2 + (z3 - z1)**2 - Ld**2 
  F[5] = (x3 - x2)**2 + (y3 - y2)**2 + (z3 - z2)**2 - Ld**2 
  F[6] = np.dot(np.cross(Upa1, Uab1),u1)
  F[7] = np.dot(np.cross(Upa2, Uab2),u2)
  F[8] = np.dot(np.cross(Upa3, Uab3),u3)
  return F    

zGuess = [1., 1., 1., 1., 1., 1., 1., 1., 1.]
z = fsolve(equations, zGuess)

输出如下:

    ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-fbac9f95877a> in <module>()
----> 1 z = fsolve(equations, zGuess)

/Users/j/anaconda2/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in fsolve(func, x0, args, fprime, full_output, col_deriv, xtol, maxfev, band, epsfcn, factor, diag)
144                'diag': diag}
145 
--> 146     res = _root_hybr(func, x0, args, jac=fprime, **options)
147     if full_output:
148         x = res['x']

/Users/j/anaconda2/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in _root_hybr(func, x0, args, jac, col_deriv, xtol, maxfev, band, eps, factor, diag, **unknown_options)
210     if not isinstance(args, tuple):
211         args = (args,)
--> 212     shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,))
213     if epsfcn is None:
214         epsfcn = finfo(dtype).eps

/Users/j/anaconda2/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
 24 def _check_func(checker, argname, thefunc, x0, args, numinputs,
 25                 output_shape=None):
---> 26     res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
 27     if (output_shape is not None) and (shape(res) != output_shape):
 28         if (output_shape[0] != 1):

<ipython-input-12-951ec1513690> in equations(z)
 18     F[4] = (x3 - x1)**2 + (y3 - y1)**2 + (z3 - z1)**2 - Ld**2
 19     F[5] = (x3 - x2)**2 + (y3 - y2)**2 + (z3 - z2)**2 - Ld**2
---> 20     F[6] = np.dot(np.cross(Upa1, Uab1),u1)
 21     F[7] = np.dot(np.cross(Upa2, Uab2),u2)
 22     F[8] = np.dot(np.cross(Upa3, Uab3),u3)

/Users/j/anaconda2/lib/python2.7/site-packages/sympy/core/expr.pyc in __float__(self)
237         if result.is_number and result.as_real_imag()[1]:
238             raise TypeError("can't convert complex to float")
--> 239         raise TypeError("can't convert expression to float")
240 
241     def __complex__(self):

TypeError: can't convert expression to float

    TypeError: can't convert expression to float

0 个答案:

没有答案