Hi. On Wed, Nov 16, 2011 at 12:19 AM, Daniel <[email protected]> wrote: > I'm trying to write a program to enhance my understanding of a > classical physics class that I am taking. I'm using vpython to create > free body diagrams and to animate kinetic motion from forces that are > described in a class I wrote. I want to turn this into more of a > general purpose solver by using sympy to solve symbolically instead of > relying on the user to enter in the magnitude and angle of each force > vector. > > I'm still getting accustomed to using sympy, but I keep running into a > problem. I can solve symbolically for one equation as in: > > a = Symbol('a') > v = Symbol('v') > t = Symbol('t') > foo = solve(a*t+v,v) # v in terms of t and a > > but I am unable to use that symbolic solution foo in another > equation. For example: > > solve(foo/t,a) > > gives me the error "unsupported operand type(s) for /: 'list' and > 'Symbol'. I think I know enough to understand what the error is > saying, but I don't know enough about sympy to intelligently approach > a solution. Is there an operator that accepts a list, or a simple > workaround I'm overlooking?
Solve returns a list, because in the general case, there are multiple solutions. To get the elements of a list, use the indexing syntax: >>> a = ['a', 'b', 'c'] >>> a[0] 'a' >>> a[1] 'b' >>> a[2] 'c' Note that the indexing starts at 0. In this case, you would do In [100]: solve(foo[0]/t,a) Out[100]: [0] See also http://docs.sympy.org/0.7.1/gotchas.html#lists. > > Basically, I want to be able to solve a system of equations when I > have an equal number of equations and unknowns. I can do this on > paper no problem, but attempting this in sympy is leaving me > scratching my head. I tried searching, perhaps I didn't use the right > search terms but I can't seem to find anything helpful. > > Any help would be greatly appreciated. You can solve a system simultaneously by passing a list of equations and a list of variables to solve for: In [102]: solve([x + y + 1, x - y - 1], [x, y]) Out[102]: {x: 0, y: -1} Aaron Meurer -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
