Expressing equation in terms of variables

934 views
Skip to first unread message

dori...@berkeley.edu

unread,
May 25, 2015, 1:07:22 AM5/25/15
to sy...@googlegroups.com
Hi all,

I am new to SymPy and I am trying to rearrange equations in order to express one variable in terms of another in SymPy.
For example, I have two physics equations F=ma and rho=m/v which I defined in the beginning. I want to re-express ``a`` in terms of f,rho, and v.
However, the ``solve`` function sets the right hand side of the equation so that it is equal to zero so then when I try to get ``a`` in terms of `f` it returns zero.
Is there a way  I could do this SymPy, if so, which function should I be looking at instead?

Thank you.



In [6]:
from sympy import *
In [11]:
f = Symbol('f')
m = Symbol('m')
a = Symbol('a')
rho = Symbol('rho')
v = Symbol('v')
In [37]:
f=m*a
rho=m*v
In [38]:
solve(a,[f])
Out[38]:
[]
In [39]:
solve(rho,[v])
Out[39]:
[0]
In [42]:
solve_linear(f-m*a,f)
Out[42]:
(m, 0)

Amit Saha

unread,
May 25, 2015, 1:29:01 AM5/25/15
to sy...@googlegroups.com
On Mon, May 25, 2015 at 5:18 AM, <dori...@berkeley.edu> wrote:
> Hi all,
>
> I am new to SymPy and I am trying to rearrange equations in order to express
> one variable in terms of another in SymPy.
> For example, I have two physics equations F=ma and rho=m/v which I defined
> in the beginning. I want to re-express ``a`` in terms of f,rho, and v.
> However, the ``solve`` function sets the right hand side of the equation so
> that it is equal to zero so then when I try to get ``a`` in terms of `f` it
> returns zero.
> Is there a way I could do this SymPy, if so, which function should I be
> looking at instead?

Is this what you want?

First find m in terms of F and a:

>>> m1 = solve(F-m*a, m, dict=True)[0][m]

Now, feed in above to the second equation:

>>> solve(rho - m1/v, a)
[F/(rho*v)]



Best,
Amit.

>
> Thank you.
>
>
>
> In [6]:
>
> from sympy import *
>
> In [11]:
>
> f = Symbol('f')
>
> m = Symbol('m')
>
> a = Symbol('a')
>
> rho = Symbol('rho')
>
> v = Symbol('v')
>
> In [37]:
>
> f=m*a
>
> rho=m*v
>
> In [38]:
>
> solve(a,[f])
>
> Out[38]:
>
> []
>
> In [39]:
>
> solve(rho,[v])
>
> Out[39]:
>
> [0]
>
> In [42]:
>
> solve_linear(f-m*a,f)
>
> Out[42]:
>
> (m, 0)
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/b3475c31-357a-4f59-abe1-a4b43a78892e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://echorand.me

Chris Smith

unread,
May 26, 2015, 10:36:53 AM5/26/15
to sy...@googlegroups.com


On Monday, May 25, 2015 at 12:07:22 AM UTC-5, dori...@berkeley.edu wrote:
Hi all,

I am new to SymPy and I am trying to rearrange equations in order to express one variable in terms of another in SymPy.
For example, I have two physics equations F=ma and rho=m/v which I defined in the beginning. I want to re-express ``a`` in terms of f,rho, and v.
However, the ``solve`` function sets the right hand side of the equation so that it is equal to zero so then when I try to get ``a`` in terms of `f` it returns zero.
Is there a way  I could do this SymPy, if so, which function should I be looking at instead?



Instead of defining a Python symbol, e.g. f = m*a, define an equation containing the symbols of interest, e.g. e1 = Eq(f, ma). Then, when solving, tell solve what you *don't* want to solve for and it will attempt to solve for the remaining symbols:

>>> e1=Eq(f,m*a)
>>> e2=Eq(rho,m*v)
>>> solve((e1,e2),exclude=[f,rho,v])  # i.e. solve for m and a
[{m: rho/v, a: f*v/rho}]
>>> 

/c
Reply all
Reply to author
Forward
0 new messages