最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Symbolic derivative with sympy of a symbolic function - Stack Overflow

programmeradmin2浏览0评论

If I define a variable e.g. $ V = f(r) * x^2 $ and I choose $f(r)$ as a symbolic function with $ r = \sqrt(x^2+y^2+z^2) $. Now I take the derivative of V with respect to x and want to get the solution from sympy, then the result is:

x**3*Subs(Derivative(f_A(_xi_1), _xi_1), _xi_1, sqrt(x**2 + y**2 + z**2))/sqrt(x**2 + y**2 + z**2) + 2*x*f_A(sqrt(x**2 + y**2 + z**2))

Its possible to get a symbolic output for the derivative like $ \frac{x^3}{r} df(r)/dx + 2x f(r) $

import sympy as sp

x, y, z = sp.symbols('x y z')
r = sp.sqrt(x**2 + y**2 + z**2)  
f_A = sp.Function('f_A')(r)  

V = f_A * x**2  

dV_dx = sp.diff(V, x)

print(dV_dx)

If I define a variable e.g. $ V = f(r) * x^2 $ and I choose $f(r)$ as a symbolic function with $ r = \sqrt(x^2+y^2+z^2) $. Now I take the derivative of V with respect to x and want to get the solution from sympy, then the result is:

x**3*Subs(Derivative(f_A(_xi_1), _xi_1), _xi_1, sqrt(x**2 + y**2 + z**2))/sqrt(x**2 + y**2 + z**2) + 2*x*f_A(sqrt(x**2 + y**2 + z**2))

Its possible to get a symbolic output for the derivative like $ \frac{x^3}{r} df(r)/dx + 2x f(r) $

import sympy as sp

x, y, z = sp.symbols('x y z')
r = sp.sqrt(x**2 + y**2 + z**2)  
f_A = sp.Function('f_A')(r)  

V = f_A * x**2  

dV_dx = sp.diff(V, x)

print(dV_dx)
Share Improve this question edited Mar 12 at 18:19 President James K. Polk 42.1k29 gold badges109 silver badges145 bronze badges asked Mar 12 at 11:18 Hendriksdf5Hendriksdf5 496 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I would create r as a function of x, y, z:

import sympy as sp

x, y, z = sp.symbols('x y z')
r = sp.Function("r")(x, y, z)
r_expr = sp.sqrt(x**2 + y**2 + z**2)
f_A = sp.Function('f_A')(r)  

V = f_A * x**2  
V
# x**2*f_A(r(x, y, z))

The differentiation produces:

V.diff(x)
# x**2*Derivative(f_A(r(x, y, z)), r(x, y, z))*Derivative(r(x, y, z), x) + 2*x*f_A(r(x, y, z))

You can then substitute the appropriate terms, like this:

V.diff(x).subs(r.diff(x), r_expr.diff(x))
# x**3*Derivative(f_A(r(x, y, z)), r(x, y, z))/sqrt(x**2 + y**2 + z**2) + 2*x*f_A(r(x, y, z))
发布评论

评论列表(0)

  1. 暂无评论