Reputation: 31
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:
Can anyone tell me which part should I change in my code?
Upvotes: 3
Views: 12052
Reputation: 11
In this line from the source code in the original question, I believe the typo e should be re-written e1
solve([e,e2,e3],x1,x2,x3)
Upvotes: 1
Reputation:
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.
Upvotes: 7
Reputation: 314
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.
Upvotes: 0