cas - Solving Equations with Python and Sympy and getting numerical answers -
i'm trying use sympy solve equations, straight numerical answer. script this:
from sympy import * a,b,v=symbols('a,b,v') eq1=eq(630.26*(v-39.0)*v*(v+39)-a+b,0) eq2=eq(b,1.36*10**8*(v-39)) eq3=eq(a,5.75*10**5*v*(v+39.0)) solve([eq1,eq2,eq3], [a,b,v], dict=true)
it gives me long list of solutions in expanded form. example,
[{v: 304.107299632956 - (-5162698.06009073 + 3004043.12120894*i)**(1/3)*(-0.5 + 0.866025403784439*i) - 32920.4469842867/((-5162698.06009073 + 3004043.12120894*i)**(1/3)*(-0.5 + 0.866025403784439*i)), b: 36054592750.082 - 1245.8292864816*i*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3) + 8.46536389385714e+17/((-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3)*(1.0 - 1.73205080756888*i)) + 719.279873914469*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3), a: 97854838797.9765 - 3957.60119254414*i*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3) - 3.13901978017549e-5*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(2/3) - 0.000285202926135405*i*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(2/3) + 2925.78725273524*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3)}, {v: 304.107299632956 - (-5162698.06009073 + 3004043.12120894*i)**(1/3) - 32920.4469842867/(-5162698.06009073 + 3004043.12120894*i)**(1/3), b: -1.05776452046245e-5*(4.0015351858068e+22 - 136000000.0*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3)*(25062979.0 - (-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3)))/(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3), a: 97854838797.9765 - 3936.45368131564*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3) + 5.56956529342379e+24/(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(2/3) + 6.43347823930771e-5*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(2/3) - 1.15822484655024e+18/(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3)}, {v: 304.107299632956 - 32920.4469842867/((-5162698.06009073 + 3004043.12120894*i)**(1/3)*(-0.5 - 0.866025403784439*i)) - (-5162698.06009073 + 3004043.12120894*i)**(1/3)*(-0.5 - 0.866025403784439*i), b: 36054592750.082 + 8.46536389385714e+17/((-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3)*(1.0 + 1.73205080756888*i)) + 719.279873914469*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3) + 1245.8292864816*i*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3), a: 97854838797.9765 + 2.31644969310047e+18/((-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3)*(1.0 + 1.73205080756888*i)) - 3.21673911965385e-5*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(2/3) + 5.57155558993486e-5*i*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(2/3) - 1.11391305868476e+25/((-4.36224183723014e+21 + 2.53827793755398e+21*i)**(2/3)*(1.0 - 1.73205080756888*i)) + 1968.22684065782*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3) + 3409.06888884012*i*(-4.36224183723014e+21 + 2.53827793755398e+21*i)**(1/3)}]
i can of course evaluate them evalf not @ once. i'm looking clean way receive solutions of equation in numerical form. i've made workaround function now. if there's better way, i'd know. function print answers follows:
def printeqsolve(input): in input: j in i: print "%r:" %j, i[j].evalf(chop=true) print "---"
i'd exclude non-real solutions, when restrict symbols real no solutions found.
you use nsolve
need give "good enough" guess , cannot pass eq instances:
>>> nsolve([e.lhs - e.rhs e in eq1,eq2,eq3], [a,b,v], [0,0,0]) matrix( [['4442890172.68209'], ['4289299466.1432'], ['70.5389666628177']]) >>> nsolve([e.lhs - e.rhs e in eq1,eq2,eq3], [a,b,v], [1e5,1e4,1e3]) matrix( [['266367838273.086'], ['84646784928.5322'], ['661.402830356854']])
Comments
Post a Comment