Reputation: 5824
from sympy import *
var('x y')
eqn=x**2+y
print(eqn.diff(x))
Getting 2*x but required 2*x+y'
Upvotes: 0
Views: 1480
Reputation: 5824
We have to initialize y as a function of x rather than a Symbol
from sympy import *
var('x')
y = Function('y')(x)
eqn = x**2 + y
x=eqn.diff(x)
print(x)
# 2*x + Derivative(y(x), x)
Source:GUser Jashan
As mention by Smichr(https://stackoverflow.com/users/1089161/smichr) ,to get dy/dx from an equation or expression = 0
from sympy import *
var('x y')
eqn = x**2 + y
x=idiff(eqn,y,x)
print(x)
# -2*x
Upvotes: 1