3

I am trying to solve 3 of trigonometric equations in python. I used Sympy library but I got a error such as 'TypeError: can't convert expression to float'

Here is My Python Source code:

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)

I added My error message:

enter image description here

Can anyone tell me which part should I change in my code?

giusti
  • 3,156
  • 3
  • 29
  • 44
Suyoung Park
  • 31
  • 1
  • 1
  • 3

2 Answers2

6

from math import * is the major error here. The functions math.sin and math.cos are for numeric computation only, you can't give them symbolic arguments. Any mathematical functions must be imported from SymPy in order to use them in symbolic computations.

Guideline: when using SymPy, don't import anything from math. Changing the import to

from sympy import *

will solve most of the issue. You'll still have to define x4 that's currently undefined.

  • Yes. It works this way. I checked. I knew that using `from math import *` could be bad decision here. Upvoted! @Suyoung Check this. It will work. – Upasana Mittal Aug 09 '18 at 17:26
  • Thanks for your answer. It doesn't show me any error anymore but It seems to take so long time to compute equations.... – Suyoung Park Aug 10 '18 at 11:02
  • @Suyoung It's a complicated system of nonlinear equations. At a glance, I would guess there is no analytic solution for it. That's a mathematical issue, not a SymPy issue. You may be able to find a numeric solution by using SciPy instead.v –  Aug 10 '18 at 13:02
0

I couldn't tell you without seeing the full code but it looks like your variable "x4" on line e3 has not been declared anywhere.

Patrick Maynard
  • 314
  • 3
  • 18