SymPy dsolve无法针对初始条件进行求解

SymPy dsolve couldn#39;t solve for initial conditions(SymPy dsolve无法针对初始条件进行求解)
本文介绍了SymPy dsolve无法针对初始条件进行求解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用SymPy的dsolve用ICS解以下微分方程:

from sympy import Function, Derivative, dsolve, symbols
t, k = symbols('t,k', real=True)

M = Function('M')(t)
M_ = Derivative(M,t)
Eqn = M_ - k*M
sol = dsolve(Eqn, ics={M.subs(t,0): 100, M.subs(t, 6): 97})

我收到以下错误:

File "..PythonPython39libsite-packagessympysolversodeode.py", line 826, in solve_ics
    raise ValueError("Couldn't solve for initial conditions")
ValueError: Couldn't solve for initial conditions

由于某些原因,SymPy很难解这个方程,这个方程很容易手工求解。

推荐答案

正如OSCAR指出的那样,dsolve无法求解ODE,因为只需要一个初始条件。然而,为了1)求解常微分方程和2)确定参数k,这两个条件都是必需的。这两个条件必须分开完成。下面的代码实现了这一点:

from sympy import Function, Derivative, dsolve, solve, symbols, Eq
t, k = symbols('t,k', real=True)

t1 = 0
M1 = 100
t2 = 6
M2 = 97

M = Function('M')(t)
M_ = Derivative(M,t)
Eqn = Eq(M_,k*M)
sol = dsolve(Eqn, ics={M.subs(t,t1): M1})

expr = sol.rhs.subs(t,t2)
eqn2 = Eq(expr,M2)
k_value = float(solve(eqn2,k)[-1])
sol_with_k = sol.subs(k,k_value)
print(sol_with_k)

下列哪项给出了正确的输出:

Eq(M(t), 100*exp(-0.00507653458078476*t))

这篇关于SymPy dsolve无法针对初始条件进行求解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)