用SymPy解二阶常线性微分方程的意外结果 [英] Unexpected result for solving ordinary linear differential equation of second order with SymPy

查看:262
本文介绍了用SymPy解二阶常线性微分方程的意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试求解这个二阶普通线性微分方程与SymPy并获得意外结果。

I am trying to solve this ordinary linear differential equation of second order with SymPy and get an unexpected result.

import sympy as sym
k, t = sym.symbols('k, t') 
s = sym.Function('s')

diff_eq = sym.Eq(s(t).diff(t, 2) + s(t) * k**2, 0) # everything fine here, when I print this I get what I expected.

solution_diff_eq = sym.dsolve(diff_eq, s(t))  
print(solution_diff_eq)

哪些印刷品

Eq(s(t), C1*exp(-I*k*t) + C2*exp(I*k*t))

但是,我期望的解决方案

有什么想法我做错了吗?

Any ideas what I have done wrong?

推荐答案

结果打印为

Eq(s(t), C1*exp(-I*k*t) + C2*exp(I*k*t))

I 是假想单位。您可能希望使用实数形式,但是没有通知sympy并产生了最简单的形式作为指数项的总和,尤其是因为 k 是否真的实数尚不清楚。

which is correct, as I is the imaginary unit. You might prefer the real form, but sympy was not notified of that and produced the most simple form as sum of exponential terms, especially as it is not clear if k is actually real.

如果通过


明确表明 k 是正实数,

If you make it explicit that k is a positive real number via

k = sym.Symbol('k', real=True, positive=True) 

该解决方案实际上是真实的形式,正如您所期望的

the solution is actually in real form, as you were expecting

Eq(s(t), C1*sin(k*t) + C2*cos(k*t))

这篇关于用SymPy解二阶常线性微分方程的意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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