Smart Manoj
Smart Manoj

Reputation: 5824

How to differentiate with dependent variables in sympy

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

Answers (1)

Smart Manoj
Smart Manoj

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

Related Questions