python - Sympy library solve to an unknown variable -
i have derived equations variables. want solve unknown variable. using sympy. code follows:
import sympy syp import math m #this unknown variable want find c0 = syp.symbol('c0') #known variables d0 = 0.874 theta2 = 10.0 fi2 = 80.0 theta1 = (theta2/180.0)*m.pi fi1 = (fi2/180.0)*m.pi #definitions of 6 different equations of them in respect co. c_t = 5*m.pi*(d0+4*c0) st123 = 1.5*theta1*(d0+2*c0) st45 = fi1*(d0+7*c0) l1 = syp.sqrt((0.5*(d0+4*c0)-0.5*d0*m.cos(theta1))**2 + (0.5*d0*m.sin(theta1))**2) l2 = syp.sqrt((0.5*(d0+6*c0)-0.5*(d0+2*c0)*m.cos(theta1))**2 + (0.5*(d0+2*c0)*m.sin(theta1))**2) l3 = syp.sqrt((0.5*(d0+8*c0)-0.5*(d0+4*c0)*m.cos(theta1))**2 + (0.5*(d0+4*c0)*m.sin(theta1))**2) #definition of general relationship between above functions. here c0 unknown , c_b c_b = c_t + 6*c0 + 3*(l1+l2+l3) - 3*st123 - 3*st45 #for c_b = 10.4866, find c0 syp.solve(c_b - 10.4866, c0) as observed, want solve c_b relationship c0. until last line code works fine. when ran whole script seems takes ages calculate c0. dont have warning message dont have solution either. suggest alternative or possible solution? lot in advance.
as have mentioned in comment problem numerical in nature, better try solve numpy/scipy. nonetheless amusing example of how numerics in sympy here 1 suggested workflow.
first of all, if not relative complexity of expressions here, scipy have been better option on sympy. expression rather complicated, can first simplify in sympy , feed scipy:
>>> c_b 38.0∗c0 +3.0∗((0.17∗c0+0.076)∗∗2+(2.0∗c0+0.0066)∗∗2)∗∗0.5 +3.0∗((0.35∗c0+0.076)∗∗2+(2.0∗c0+0.0066)∗∗2)∗∗0.5 +3.0∗((2.0∗c0+0.0066)∗∗2+0.0058)∗∗0.5 +9.4 >>> simplify(c_b) 38.0∗c0 +3.0∗(4.0∗c0∗∗2+0.027∗c0+0.0058)∗∗0.5 +3.0∗(4.1∗c0∗∗2+0.053∗c0+0.0058)∗∗0.5 +3.0∗(4.2∗c0∗∗2+0.08∗c0+0.0058)∗∗0.5 +9.4 now given not interested in symbolics , simplification not good, useless continue using sympy instead of scipy, if insist can it.
>>> nsolve(c_b - 10.4866, c0, 1) # numerical solution 0.00970963412692139 if try use solve instead of nsolve waste lot of resources in searching symbolic solution (that may not exist in elementary terms) when numeric 1 instantaneous.
Comments
Post a Comment