sympy – an example of implicit differentiation


from sympy import *

def pprints(func, *funcs):
    pprint(func),
    if funcs is None: return
    for f in funcs:
        pprint(f),

init_printing()


# 2.6 Example 2, p165
# Find the slope of circle x**2 + y**2 = 25 at the point (3, -4).

x = symbols('x')
y = Function('y')(x)
z = symbols('z')

# change the circle formula to an equation 
# x**2 + y**2 - 25 = 0
eq = x**2 + y**2 - 25

# Make a differential equation
d_eq = diff(eq, x)

roots = solve(d_eq, diff(y, x, 1))
_dydx = roots[0]
pprints(eq, d_eq, _dydx)

# access y(x) term
# from sympy.core.function import AppliedUndef
# _y = iter(dydx.atoms(AppliedUndef)).next()

# replace y(x) with z 
dydx = _dydx.subs(Function('y')(x), z)
slope = dydx.subs([(x, 3), (z, -4)])
print 'The slope is ', slope
=== Outupt ===
2    2        
x  + y (x) - 25
             d       
2⋅x + 2⋅y(x)⋅──(y(x))
             dx      
-x  
────
y(x)
The slope is  3/4

About janpenguin

Email: janpenguin [at] riseup [dot] net Every content on the blog is made by Free and Open Source Software in GNU/Linux.
This entry was posted in Python and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s