简体   繁体   English

如何找到SymPy给出一阶导数的n阶导数?

[英]How to find the nth derivative given the first derivative with SymPy?

Given some f and the differential equation x '( t ) = f ( x ( t )), how do I compute x ( n ) ( t ) in terms of x ( t )? x(t)的形式给出一些f和差分方程x“(T)= F(X(t)) ,如何计算x(n) (t)的

For example, given f ( x ( t )) = sin( x ( t )), I want to obtain x (3) ( t ) = (cos( x ( t )) 2 − sin( x ( t )) 2 ) sin( x ( t )). 例如,给定fxt ))= sin( xt )),我想得到x (3)t )=(cos( xt )) 2 - sin( xt )) 2 )sin( xt ))。

So far I've tried 到目前为止我已经尝试过了

>>> from sympy import diff, sin
>>> from sympy.abc import x, t
>>> diff(sin(x(t)), t, 2)

which gives me 这给了我

-sin(x(t))*Derivative(x(t), t)**2 + cos(x(t))*Derivative(x(t), t, t)

but I'm not sure how to tell SymPy what Derivative(x(t), t) is and have it figure out Derivative(x(t), t, t) , etc. automatically. 但我不知道如何告诉SymPy Derivative(x(t), t)是什么,让它自动计算出Derivative(x(t), t, t)等。


Answer: 回答:

Here's my final solution based on the answers I received below: 以下是我根据下面收到的答案提出的最终解决方案:

def diff(x_derivs_known, t, k, simplify=False):
    try: n = len(x_derivs_known)
    except TypeError: n = None
    if n is None:
        result = sympy.diff(x_derivs_known, t, k)
        if simplify: result = result.simplify()
    elif k < n:
        result = x_derivs_known[k]
    else:
        i = n - 1
        result = x_derivs_known[i]
        while i < k:
            result = result.diff(t)
            j = len(x_derivs_known)
            x0 = None
            while j > 1:
                j -= 1
                result = result.subs(sympy.Derivative(x_derivs_known[0], t, j), x_derivs_known[j])
            i += 1
            if simplify: result = result.simplify()
    return result

Example: 例:

>>> diff((x(t), sympy.sin(x(t))), t, 3, True)
sin(x(t))*cos(2*x(t))

Declare f and use substitution: 声明f并使用替换:

>>> f = diff(x(t))
>>> diff(sin(x(t)), t, 2).subs(f, sin(x(t)))
-sin(x(t))**3 + cos(x(t))*Derivative(sin(x(t)), t)

Here is one approach that returns a list of all derivatives up to n -th order 这是一种方法,它返回所有导数的列表,直到n

import sympy as sp

x = sp.Function('x')
t = sp.symbols('t')

f = lambda x: x**2 #sp.exp, sp.sin
n = 4 #3, 4, 5

deriv_list = [x(t), f(x(t))]  # list of derivatives [x(t), x'(t), x''(t),...]
for i in range(1,n):
    df_i = deriv_list[-1].diff(t).replace(sp.Derivative,lambda *args: f(x(t)))
    deriv_list.append(df_i)

print(deriv_list)

[x(t), x(t)**2, 2*x(t)**3, 6*x(t)**4, 24*x(t)**5]

With f=sp.sin it returns 随着f=sp.sin它返回

  [x(t), sin(x(t)), sin(x(t))*cos(x(t)), -sin(x(t))**3 + sin(x(t))*cos(x(t))**2, -5*sin(x(t))**3*cos(x(t)) + sin(x(t))*cos(x(t))**3] 

EDIT: A recursive function for the computation of the n -th derivative: 编辑:计算第n个导数的递归函数:

def der_xt(f, n):
    if n==1:
        return f(x(t))
    else:
        return der_xt(f,n-1).diff(t).replace(sp.Derivative,lambda *args: f(x(t)))

print(der_xt(sp.sin,3))

-sin(x(t))**3 + sin(x(t))*cos(x(t))**2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM