简体   繁体   English

Sympy函数导数和方程组

[英]Sympy function derivatives and sets of equations

I'm working with nonlinear systems of equations. 我正在使用非线性方程组。 These systems are generally a nonlinear vector differential equation. 这些系统通常是非线性矢量微分方程。 I now want to use functions and derive them with respect to time and to their time-derivatives, and find equilibrium points by solving the nonlinear equations 0=rhs(eqs). 我现在想使用函数并根据时间及其时间导数推导它们,并通过求解非线性方程0 = rhs(eqs)来找到平衡点。 Similar things are needed to calculate the Euler-Lagrange equations, where you need the derivative of L wrt. 计算Euler-Lagrange方程需要类似的事情,在这里您需要L wrt的导数。 diff(x,t). diff(x,t)。

Now my question is, how do I implement this in Sympy? 现在我的问题是,如何在Sympy中实现这一点? My main 2 problems are, that deriving a Symbol f wrt. 我的两个主要问题是推导符号f wrt。 t diff(f,t) , I get 0. I can see, that with t diff(f,t) ,我得到0。我可以看到

x = Symbol('x',real=True);
diff(x.subs(x,x(t)),t) # because diff(x,t) => 0

and

diff(x**2, x)

does kind of work. 做某种工作。

However, with 但是,随着

x = Fuction('x')(t);
diff(x,t);

I get this to work, but I cannot differentiate wrt. 我可以使用它,但是我无法区分wrt。 the funtion x itself, like 函数x本身,例如

diff(x**2,x) -DOES NOT WORK.

Since I need these things, especially not only for scalars, but for vectors (using jacobian) all the time, I really want this to be a clean and functional workflow. 因为我需要这些东西,尤其是不仅需要标量,而且一直需要向量(使用jacobian),所以我真的希望这是一个干净且实用的工作流程。 Which kind of type should I initiate my mathematical functions in Sympy in order to avoid strange substitutions? 为了避免奇怪的替换,我应该在Sympy中启动哪种数学函数? It only gets worse for matricies, where I cannot get 对于只有我无法得到的矩阵,情况只会变得更糟

eqns = Matrix([f1-5, f2+1]);
variabs = Matrix([f1,f2]);
nonlinsolve(eqns,variabs);

to work as expected, since it only allows symbols as input. 可以正常工作,因为它只允许将符号作为输入。 Is there an easy conversion here? 这里有一个简单的转换吗? Like eqns.tolist() - which doesn't work either? eqns.tolist() -哪个也不起作用?

EDIT: I just found this question, which was answered towards using expressions and matricies. 编辑:我刚刚发现了这个问题,这是对使用表达式和矩阵的回答。 I want to be able to solve sets of nonlinear equations, build the jacobian of a vector wrt. 我希望能够解决非线性方程组,建立向量wrt的雅可比。 another vector and derive wrt. 另一个向量并得出wrt。 functions as stated above. 如上所述的功能。 Can anyone point me into a direction to start a concise workflow for this purpose? 谁能为此目的指示我启动一个简洁的工作流程的方向? I guess the most complex task is calculating the Lie-derivative wrt. 我猜最复杂的任务是计算李导数wrt。 a vector or list of functions, the rest should be straight forward. 向量或函数列表,其余的应该简单明了。

Edit 2: 编辑2:

def substi(expr,variables): 
   return expr.subs( {w:w(t)} )

would automate the subsitution, such that substi(vector_expr,varlist_vector).diff(t) is not all 0. 会自动执行substi(vector_expr,varlist_vector).diff(t)并非全为0。

The following defines x to be a function of t 以下将x定义为t的函数

import sympy as s
t = s.Symbol('t')    

x = s.Function('x')(t)

This should solve your problem of diff(x,t) being evaluated as 0 . 这应该可以解决diff(x,t)被评估为0 But I think you will still run into problems later on in your calculations. 但是我认为您在以后的计算中仍然会遇到问题。 I also work with calculus of variations and Euler-Lagrange equations. 我还处理变量和Euler-Lagrange方程的演算。 In these calculations, x' needs to be treated as independent of x . 在这些计算中,需要将x'视为独立于x So, it is generally better to use two entirely different variables for x and x' so as not to confuse Sympy with the relationship between those two variables. 因此,通常最好对xx'使用两个完全不同的变量,以免使Sympy与这两个变量之间的关系混淆。 After we are done with the calculations in Sympy and we go back to our pen and paper we can substitute x' for the second variable. 在完成了Sympy中的计算并返回到笔和纸之后,我们可以用x'代替第二个变量。

Yes, one has to insert an argument in a function before taking its derivative. 是的,必须在获取函数派生参数之前在函数中插入参数。 But after that, differentiation with respect to x(t) works for me in SymPy 1.1.1, and I can also differentiate with respect to its derivative. 但是之后,在SymPy 1.1.1中,对x(t)微分对我有用,并且我也可以对它的导数进行微分。 Example of Euler-Lagrange equation derivation: Euler-Lagrange方程推导的示例:

t = Symbol("t")
x = Function("x")(t)
L = x**2 + diff(x, t)**2    # Lagrangian
EL = -diff(diff(L, diff(x, t)), t) + diff(L, x)

Now EL is 2*x(t) - 2*Derivative(x(t), t, t) as expected. 现在EL为2*x(t) - 2*Derivative(x(t), t, t)

That said, there is a build-in method for Euler-Lagrange : 也就是说, Euler-Lagrange有一个内置方法

EL = euler_equations(L)

would yield the same result, except presented as a differential equation with right-hand side 0: [Eq(2*x(t) - 2*Derivative(x(t), t, t), 0)] 会产生相同的结果,除了用右侧为0的微分方程[Eq(2*x(t) - 2*Derivative(x(t), t, t), 0)][Eq(2*x(t) - 2*Derivative(x(t), t, t), 0)]

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

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