Reputation: 872
I would like to know how to solve a non-linear system of equations with respect to a certain variable. Consider this example:
import sympy as sp
from sympy.solvers import solve
t2, t3, c = sp.symbols('theta_2 theta_3 c')
f1 = 2*sp.cos(t2) - 10*sp.cos(t3) - c
f2 = 2*sp.sin(t2) - 10*sp.sin(t3)
solve([f1, f2], c)[c]
Any ideas?
Upvotes: 0
Views: 283
Reputation: 14530
If you specify theta3 as an unknown then solve will eliminate it from the solution for c:
In [9]: solve([f1, f2], [c, t3], dict=True)
Out[9]:
⎡⎧ _______________ ⎫ ⎧ _______________ ⎫⎤
⎢⎪ ╱ 2 ⎛sin(θ₂)⎞⎪ ⎪ ╱ 2 ⎛sin(θ₂)⎞⎪⎥
⎢⎨c: - 2⋅╲╱ cos (θ₂) + 24 + 2⋅cos(θ₂), θ₃: asin⎜───────⎟⎬, ⎨c: 2⋅╲╱ cos (θ₂) + 24 + 2⋅cos(θ₂), θ₃: π - asin⎜───────⎟⎬⎥
⎢⎪ ⎝ 5 ⎠⎪ ⎪ ⎝ 5 ⎠⎪⎥
⎣⎩ ⎭ ⎩ ⎭⎦
Now you can extract the solutions for c only:
In [10]: sol = solve([f1, f2], [c, t3], dict=True)
In [11]: [s[c] for s in sol]
Out[11]:
⎡ _______________ _______________ ⎤
⎢ ╱ 2 ╱ 2 ⎥
⎣- 2⋅╲╱ cos (θ₂) + 24 + 2⋅cos(θ₂), 2⋅╲╱ cos (θ₂) + 24 + 2⋅cos(θ₂)⎦
The output that I show here is from isympy which can print nicely if you use a unicode capable terminal making code and output that can be copy-pasted into something like SO. That's better and quicker than using screenshots.
Upvotes: 1