Implicit Differentiation

41 views
Skip to first unread message

Andrew Hidden

unread,
Nov 4, 2009, 9:54:35 PM11/4/09
to sympy
I was reading another post here on Implicit Differentiation however it
was not quiet what I was looking for. I am just wondering how to do it
in a more simple way like finding dy/dx for x^2+y^2 - 36 Is there
something I dont know of like implicit_diff() <-- (I wish)?

Thanks,
Andrew

smichr

unread,
Nov 5, 2009, 8:17:07 PM11/5/09
to sympy
I'm not sure which post you looked at, but does the following do
what you are hoping?

###
>>> import sympy as s
>>> eq=s.sympify('x^2+y^2 - 36')
>>> f=s.Function('f')
>>> eq=eq.subs(y,f(x))
>>> [ans.subs(f(x), y) for ans in s.solve(eq.diff(x), f(x).diff(x))]
[-x/y]
###

/c

smichr

unread,
Nov 5, 2009, 9:33:25 PM11/5/09
to sympy
p.s. just remembering that you will always just get a single solution
for dy/dx so you can return the answer rather than a list. It can all
be encapsulated as:

def implicit_diff(eq, dep, ind):
"""Return d_dep/d_ind given eq(y) = 0

>>> import sympy as s
>>> s.var('x y')
(x, y)
>>> eq=s.sympify('x^2+y^2 - 36')
>>> implicit_diff(eq, y, x)
>>> -x/y
"""
f=sympy.Function('f', dummy=True)
eq=eq.subs(dep, f(ind))
return sympy.solve(eq.diff(ind), f(ind).diff(ind))[0].subs(f(ind),
dep)

Aaron S. Meurer

unread,
Nov 5, 2009, 11:13:48 PM11/5/09
to sy...@googlegroups.com
I think there is no need to run solve. The function will always be
linear in dy/dx, because it is a linear operator, so the solution will
always be -diff(eq, ind)/diff(eq, dep). See http://en.wikipedia.org/wiki/Implicit_differentiation#Formula_for_two_variables

Aaron Meurer

smichr

unread,
Nov 7, 2009, 11:05:32 PM11/7/09
to sympy


On Nov 6, 9:13 am, "Aaron S. Meurer" <asmeu...@gmail.com> wrote:
> I think there is no need to run solve.  The function will always be  
> linear in dy/dx, because it is a linear operator, so the solution will  
> always be -diff(eq, ind)/diff(eq, dep).  Seehttp://en.wikipedia.org/wiki/Implicit_differentiation#Formula_for_two...
>

That's such a nice property--dazzlingly simple!

/c
Reply all
Reply to author
Forward
0 new messages