5

How can we derivate a implicit equation in Python 3?
Example x^2+y^2=25 differentiation is: dy/dx=-x/y, when try this:

from sympy import *

init_printing(use_unicode=True)

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

eq = x**2+y**2-25
sol = diff(eq, x)
print(sol)

But it shows:

2*x + 2*y(x)*Derivative(y(x), x)

How can get -x/y?

Robby
  • 269
  • 4
  • 8

3 Answers3

7

SymPy has the function idiff which does what you want

In [2]: idiff(x**2+y**2-25, y, x)
Out[2]:
-x
───
 y
asmeurer
  • 86,894
  • 26
  • 169
  • 240
2

You may use the implicit function theorem which states that when two variables x, y, are related by the implicit equation f(x, y) = 0, then the derivative of y with respect to x is equal to - (df/dx) / (df/dy) (as long as the partial derivatives are continuous and df/dy != 0).

x, y = symbols('x, y')
f = x**2 + y**2 - 25
-diff(f,x)/diff(f,y)
-x/y
Stelios
  • 5,271
  • 1
  • 18
  • 32
1

You have the differential equation, so you can rearrange it using solve:

solve(sol, diff(y, x, 1))
zarak
  • 2,933
  • 3
  • 23
  • 29