Suy*_*ark 3 python math sympy equation-solving
我正在尝试在 python 中解决 3 个三角方程。我使用了 Sympy 库,但出现错误,例如“TypeError: can't convert expression to float”
这是我的 Python 源代码:
from sympy import Symbol, solve, Eq
from math import*
# Robot Arm length
L1 = 0
L2 = 97.9
L3 = 120
L4 = 120
L5 = 184
# location
x = L1+L2+L3+L4+L5
y = 0
z = 0
x1 = Symbol('x1',real = True)
x2 = Symbol('x2',real = True)
x3 = Symbol('x3',real = True)
#trigonometric equations
e1= Eq(L1 - (5*sin(x1))/2 - L4*(cos(x1)*sin(x2)*sin(x3) - cos(x1)*cos(x2)*cos(x3)) - L5*(cos(x4)*(cos(x1)*sin(x2)*sin(x3) - cos(x1)*cos(x2)*cos(x3)) + sin(x4)*(cos(x1)*cos(x2)*sin(x3) + cos(x1)*cos(x3)*sin(x2))) + L2*cos(x1) + L3*cos(x1)*cos(x2) - x)
e2= Eq((5*cos(x1))/2 + L4*(cos(x2)*cos(x3)*sin(x1) - sin(x1)*sin(x2)*sin(x3)) + L5*(cos(x4)*(cos(x2)*cos(x3)*sin(x1) - sin(x1)*sin(x2)*sin(x3)) - sin(x4)*(cos(x2)*sin(x1)*sin(x3) + cos(x3)*sin(x1)*sin(x2))) + L2*sin(x1) + L3*cos(x2)*sin(x1) - y)
e3= Eq(-L4*(cos(x2)*sin(x3) + cos(x3)*sin(x2)) - L3*sin(x2) - L5*(cos(x4)*(cos(x2)*sin(x3) + cos(x3)*sin(x2)) + sin(x4)*(cos(x2)*cos(x3) - sin(x2)*sin(x3))) - z)
solve([e,e2,e3],x1,x2,x3)
x1 = degrees(x1)
x2 = degrees(x2)
x3 = degrees(x3)
print("degree values : ",x1,x2,x3)
Run Code Online (Sandbox Code Playgroud)
我添加了我的错误消息:

谁能告诉我应该在代码中更改哪一部分?
小智 5
from math import *是这里的主要错误。函数math.sin和math.cos仅用于数值计算,您不能为它们提供符号参数。任何数学函数都必须从 SymPy 导入才能在符号计算中使用它们。
指南:使用 SymPy 时,不要从math. 将导入更改为
from sympy import *
Run Code Online (Sandbox Code Playgroud)
将解决大部分问题。您仍然必须定义x4当前未定义的。