如何使用SymPy在给定一阶导数的情况下找到第n阶导数? [英] How to find the nth derivative given the first derivative with SymPy?

查看:33
本文介绍了如何使用SymPy在给定一阶导数的情况下找到第n阶导数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一些 f 和微分方程 x'(t) = f(x(t)),我如何计算 x(n)(t) 在 x(t) 方面?

例如,给定 f(x(t)) = sin(x(t)),我想获得 x(3)(t) = (cos(x(t))2 − sin(x(t))2) sin(x(t)).

到目前为止我已经尝试过

<预><代码>>>>从 sympy 导入差异,罪>>>从 sympy.abc 导入 x, t>>>差异(罪(x(t)),t,2)

这给了我

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

但我不知道如何告诉 SymPy Derivative(x(t), t) 是什么并让它弄清楚 Derivative(x(t), t, t) 等自动.


答案:

这是根据我在下面收到的答案得出的最终解决方案:

def diff(x_derivs_known, t, k,simple=False):尝试:n = len(x_derivs_known)除了类型错误:n = 无如果 n 是 None:结果 = sympy.diff(x_derivs_known, t, k)如果简化:result = result.simplify()elif k <ñ:结果 = x_derivs_known[k]别的:我 = n - 1结果 = x_derivs_known[i]当我 <克:结果 = 结果.diff(t)j = len(x_derivs_known)x0 = 无当 j >1:j -= 1结果 = result.subs(sympy.Derivative(x_derivs_known[0], t, j), x_derivs_known[j])我 += 1如果简化:result = result.simplify()返回结果

示例:

<预><代码>>>>diff((x(t), sympy.sin(x(t))), t, 3, True)sin(x(t))*cos(2*x(t))

解决方案

这是一种返回所有导数的列表的方法,最多为 n

import sympy as spx = sp.Function('x')t = sp.symbols('t')f = lambda x: x**2 #sp.exp, sp.sinn = 4 #3, 4, 5deriv_list = [x(t), f(x(t))] # 导数列表 [x(t), x'(t), x''(t),...]对于范围内的 i (1,n):df_i = deriv_list[-1].diff(t).replace(sp.Derivative,lambda *args: f(x(t)))deriv_list.append(df_i)打印(deriv_list)

<块引用>

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

使用 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]

用于计算 n 次导数的递归函数:

def der_xt(f, n):如果 n==1:返回 f(x(t))别的:返回 der_xt(f,n-1).diff(t).replace(sp.Derivative,lambda *args: f(x(t)))打印(der_xt(sp.sin,3))

<块引用>

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

Given some f and the differential equation x'(t) = f(x(t)), how do I compute x(n)(t) in terms of x(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)).

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.


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))

解决方案

Here is one approach that returns a list of all derivatives up to n-th order

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

 [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:

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

这篇关于如何使用SymPy在给定一阶导数的情况下找到第n阶导数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆