On Nov 20, 12:48 am, Chris Smith <smi...@gmail.com> wrote: > On Sun, Nov 20, 2011 at 10:41 AM, litghost <litgh...@gmail.com> wrote: > > I am new to sympy, and trying to utilize solve to solve systems of > > linear equations. However there a couple things I have been unable to > > figure out. > > > The most important is how get diagnostic information about why the > > solve call failed (returned None). The system of equations I am > > solving for are pretty simple, I am problem just miss using the > > interface. However with the only output of the system being returning > > None, I have no way to figure out my mistake. > > > So here is an example I am trying: > > > from sympy import Symbol, solve > > C = Symbol('C', constant=True, real=True) > > R = Symbol('R', constant=True, real=True) > > Rf = Symbol('Rf', constant=True, real=True) > > Ri = Symbol('Ri', constant=True, real=True) > > s = Symbol('s') > > V1 = Symbol('V1') > > Vminus = Symbol('Vminus') > > Vplus = Symbol('Vplus') > > Vout = Symbol('Vout') > > The assumption 'constant=True' doesn't have a meaning to sympy. Also, > since you are not solving this with any values (everything is > symbolic) you don't have to put assumptions about the answer being > real on your symbols. Just create your variables... > > >>> var('R, C, Ri, Vout, V1, Rf, Vminus, Vplus, s') > > (R, C, Ri, Vout, V1, Rf, Vminus, Vplus, s) > > define your equations... > > >>> eqs = [C*V1*s + Vplus*(-2*C*s - 1/R), > > ... Vminus*(-1/Ri - 1/Rf) + Vout/Rf, > ... C*Vplus*s + V1*(-C*s - 1/R) + Vout/R, > ... -Vminus + Vplus] > ... > > > > Now, if you just want *some* solution, don't list any variables: > > >>> solve(eqs) > > [{Vminus: Vplus, Vout: (V1**2 - V1*Vplus - Vplus**2)/(V1 - 2*Vplus), R: > Vplus/(C > *s*(V1 - 2*Vplus)), Ri: Rf*Vplus*(V1 - 2*Vplus)/(V1 - Vplus)**2}] > > If you want to solve some equation for a single variable, use 'manual' > > >>> solve(eqs, Vout, manual=True) > > [(Vminus*(Rf + Ri)/Ri,)] > > > What I want to see is: > > Vout = Vplus*(s**2*C**2 + 3*s*C/R + 1/R^2)/(s*C/R) > > Ahh... then that means you want the solution (lhs) to *not* contain > those values. So what *do* you want to allow on the lhs? > > Here are all the variables in the equations... > > >>> vall=reduce(set.union, [e.free_symbols for e in eqs]) > >>> vall > > set([R, C, Ri, Vout, V1, Rf, Vminus, Vplus, s]) > > You *don't* want the variables in the rhs of what you just indicated > to be on the lhs of the answers... > > >>> dont = (Vplus*(s**2*C**2 + 3*s*C/R + 1/R**2)/(s*C/R)).free_symbols > >>> dont > > set([R, Vplus, s, C]) > > So what you will accept on the lhs is anything else... > > >>> want = vall - dont > >>> want > > set([Vminus, Vout, V1, Rf, Ri]) > > So try that: > > >>> solve(eqs,want) > > [{Vminus: Vplus, Vout: Vplus*(C**2*R**2*s**2 + 3*C*R*s + 1)/(C*R*s), V1: > Vplus*( > 2*C*R*s + 1)/(C*R*s), Rf: 0}, {Vminus: Vplus, Vout: Vplus*(C**2*R**2*s**2 + > 3*C* > R*s + 1)/(C*R*s), V1: Vplus*(2*C*R*s + 1)/(C*R*s), Rf: Ri*(C*R*s + > 1)**2/(C*R*s) > > }] > > And there you go: > > >>> for k, v in _[0].items(): > > ... print k, > ... print '\t',v > ... > Vminus Vplus > Vout Vplus*(C**2*R**2*s**2 + 3*C*R*s + 1)/(C*R*s) > V1 Vplus*(2*C*R*s + 1)/(C*R*s) > Rf 0 > > > When I worked it by hand there was another solution > > Vout = Vminus*(1/Ri + 1/Rf)*Rf > > so that is likely the problem. However is there any way for sympy to > > give me both solutions? Or at least indicate that was the problem? > > You have to give it direction...what do you want your solution in > terms of. We might have gotten this other solution by saying we don't > want any of those rhs variables (this is going to get ugly, but bear > through til the end): > > Like before, define what you don't want solved for: > > >>> dont=(Vminus*(1/Ri + 1/Rf)*Rf).free_symbols > >>> want = vall - dont > >>> solve(eqs,want) > > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > ...more error messages > raise CoercionFailed("can't convert %s of type %s to %s" % (a, K0, K1)) > sympy.polys.polyerrors.CoercionFailed: can't convert > DMF(([[[[[[PythonRationalTy > pe(-1, 1)], []]], [[[PythonRationalType(2, 1)], []], [[PythonRationalType(-1, > 1) > , PythonRationalType(0, 1)]]]]]], [[[[[[PythonRationalType(2, 1)]]]]]]), QQ) > of > type QQ(s,C,Rf,Ri,Vminus,sqrt(Rf**2*Vminus**2 - 4*Rf*Ri*Vminus**2)) to > QQ[s,C,Rf > ,Ri,Vminus,sqrt(Rf**2*Vminus**2 - 4*Rf*Ri*Vminus**2)] > > So go ahead and use the manual option: > > >>> solve(eqs,want,manual=True) > > [{Vplus: Vminus, Vout: Vminus*(Rf + Ri)/Ri, V1: (Rf*Vminus + 2*Ri*Vminus - > sqrt( > Rf**2*Vminus**2 - 4*Rf*Ri*Vminus**2))/(2*Ri), C: Vminus/(-2*R*Vminus*s + > R*s*(Rf > *Vminus + 2*Ri*Vminus - sqrt(Rf**2*Vminus**2 - 4*Rf*Ri*Vminus**2))/(2*Ri))}, > {Vp > lus: Vminus, Vout: Vminus*(Rf + Ri)/Ri, V1: (Rf*Vminus + 2*Ri*Vminus + > sqrt(Rf** > 2*Vminus**2 - 4*Rf*Ri*Vminus**2))/(2*Ri), C: Vminus/(-2*R*Vminus*s + > R*s*(Rf*Vmi > nus + 2*Ri*Vminus + sqrt(Rf**2*Vminus**2 - 4*Rf*Ri*Vminus**2))/(2*Ri))}] > > And again, there you go: > > >>> _[0][Vout] > > Vminus*(Rf + Ri)/Ri > > Write if you have more questions. I'm not sure why the None is > returned in this case, buthopefully you have a better idea to get what > you want and what you 'dont', > > Chris
This is pretty much exactly the kind of information I was looking for. I figured those kinds of features existed, I was just unable to locate them with the documentation. Is there a documentation page that expands on what you are talking about, so in the future I know where to start? Keith -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sympy@googlegroups.com. To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.