Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to solve trigonometric equations on python

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?

like image 627
Suyoung Park Avatar asked Jul 23 '26 00:07

Suyoung Park


1 Answers

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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!